Skip to content

Commit c969fba

Browse files
committed
[FIX] spreadsheet_edition: auto-complete with date granularity
Steps to reproduce: - Go to CRM lead pivot view - group by any date field by day - insert in spreadsheet - start typing '=odoo.pivot(1, <measure>, ' => Currently, when the auto-complete proposes a date field (e.g. "create_date"), the field is proposed without the granularity ("create_date", not "create_date:month"). The formula result is an error if the granularity is missing. closes #4231 Task: 3823433 X-original-commit: odoo/enterprise@a52b118 Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
1 parent 4e86cab commit c969fba

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

src/helpers/pivot/pivot_helpers.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { average, countAny, max, min } from "../../functions/helper_statistical"
88
import { inferFormat } from "../../functions/helpers";
99
import { _t } from "../../translation";
1010
import { Arg, CellValue, FPayload, Format, Locale, Matrix } from "../../types";
11-
import { DomainArg, PivotCoreDimension, PivotField } from "../../types/pivot";
11+
import { DomainArg, Granularity, PivotCoreDimension, PivotField } from "../../types/pivot";
1212

1313
const PIVOT_FUNCTIONS = ["PIVOT.VALUE", "PIVOT.HEADER", "PIVOT"];
1414

@@ -163,13 +163,14 @@ export function isDateField(field: PivotField) {
163163
* Create a proposal entry for the compose autocomplete
164164
* to insert a field name string in a formula.
165165
*/
166-
export function makeFieldProposal(field: PivotField) {
167-
const quotedFieldName = `"${field.name}"`;
166+
export function makeFieldProposal(field: PivotField, granularity?: Granularity) {
167+
const groupBy = granularity ? `${field.name}:${granularity}` : field.name;
168+
const quotedGroupBy = `"${groupBy}"`;
168169
return {
169-
text: quotedFieldName,
170+
text: quotedGroupBy,
170171
description: field.string + (field.help ? ` (${field.help})` : ""),
171-
htmlContent: [{ value: quotedFieldName, color: tokenColors.STRING }],
172-
fuzzySearchKey: field.string + quotedFieldName, // search on translated name and on technical name
172+
htmlContent: [{ value: quotedGroupBy, color: tokenColors.STRING }],
173+
fuzzySearchKey: field.string + quotedGroupBy, // search on translated name and on technical name
173174
};
174175
}
175176

src/registries/auto_completes/pivot_auto_complete.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
makeFieldProposal,
1010
} from "../../helpers/pivot/pivot_helpers";
1111
import { _t } from "../../translation";
12+
import { Granularity } from "../../types";
1213
import { autoCompleteProviders } from "./auto_complete_registry";
1314

1415
autoCompleteProviders.add("pivot_ids", {
@@ -119,7 +120,8 @@ autoCompleteProviders.add("pivot_group_fields", {
119120
if (!fields) {
120121
return;
121122
}
122-
const { columns, rows, type } = this.getters.getPivotCoreDefinition(pivotId);
123+
const { type } = this.getters.getPivotCoreDefinition(pivotId);
124+
const { columns, rows } = dataSource.definition;
123125
if (!supportedPivotExplodedFormulaRegistry.get(type)) {
124126
return [];
125127
}
@@ -131,8 +133,8 @@ autoCompleteProviders.add("pivot_group_fields", {
131133
args = args.filter((ast, index) => index % 2 === 1); // keep only the field names
132134
}
133135
const argGroupBys = args.map((ast) => ast?.value).filter(isDefined);
134-
const colFields = columns.map((groupBy) => groupBy.name);
135-
const rowFields = rows.map((groupBy) => groupBy.name);
136+
const colFields = columns.map((groupBy) => groupBy.nameWithGranularity);
137+
const rowFields = rows.map((groupBy) => groupBy.nameWithGranularity);
136138

137139
const proposals: string[] = [];
138140
const previousGroupBy = ["ARG_SEPARATOR", "SPACE"].includes(tokenAtCursor.type)
@@ -154,16 +156,18 @@ autoCompleteProviders.add("pivot_group_fields", {
154156
const groupBys = proposals.filter(isDefined);
155157
return groupBys
156158
.map((groupBy) => {
157-
const field = fields[groupBy];
158-
return field ? makeFieldProposal(field) : undefined;
159+
const [fieldName, granularity] = groupBy.split(":");
160+
const field = fields[fieldName];
161+
return field ? makeFieldProposal(field, granularity as Granularity) : undefined;
159162
})
160163
.concat(
161164
groupBys.map((groupBy) => {
162-
const field = fields[groupBy];
165+
const fieldName = groupBy.split(":")[0];
166+
const field = fields[fieldName];
163167
if (!field) {
164168
return undefined;
165169
}
166-
const positionalFieldArg = `"#${field.name}"`;
170+
const positionalFieldArg = `"#${groupBy}"`;
167171
const positionalProposal = {
168172
text: positionalFieldArg,
169173
description:
@@ -229,7 +233,7 @@ autoCompleteProviders.add("pivot_group_values", {
229233
if (!groupByField) {
230234
return;
231235
}
232-
return dataSource.getPossibleFieldValues(groupByField).map(({ value, label }) => {
236+
return dataSource.getPossibleFieldValues(groupByField.split(":")[0]).map(({ value, label }) => {
233237
const isString = typeof value === "string";
234238
const text = isString ? `"${value}"` : value.toString();
235239
const color = isString ? tokenColors.STRING : tokenColors.NUMBER;

0 commit comments

Comments
 (0)