Skip to content

Commit

Permalink
[FIX] spreadsheet_edition: auto-complete with date granularity
Browse files Browse the repository at this point in the history
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) <lul@odoo.com>
  • Loading branch information
LucasLefevre committed May 21, 2024
1 parent 4e86cab commit c969fba
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
13 changes: 7 additions & 6 deletions src/helpers/pivot/pivot_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { average, countAny, max, min } from "../../functions/helper_statistical"
import { inferFormat } from "../../functions/helpers";
import { _t } from "../../translation";
import { Arg, CellValue, FPayload, Format, Locale, Matrix } from "../../types";
import { DomainArg, PivotCoreDimension, PivotField } from "../../types/pivot";
import { DomainArg, Granularity, PivotCoreDimension, PivotField } from "../../types/pivot";

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

Expand Down Expand Up @@ -163,13 +163,14 @@ export function isDateField(field: PivotField) {
* Create a proposal entry for the compose autocomplete
* to insert a field name string in a formula.
*/
export function makeFieldProposal(field: PivotField) {
const quotedFieldName = `"${field.name}"`;
export function makeFieldProposal(field: PivotField, granularity?: Granularity) {
const groupBy = granularity ? `${field.name}:${granularity}` : field.name;
const quotedGroupBy = `"${groupBy}"`;
return {
text: quotedFieldName,
text: quotedGroupBy,
description: field.string + (field.help ? ` (${field.help})` : ""),
htmlContent: [{ value: quotedFieldName, color: tokenColors.STRING }],
fuzzySearchKey: field.string + quotedFieldName, // search on translated name and on technical name
htmlContent: [{ value: quotedGroupBy, color: tokenColors.STRING }],
fuzzySearchKey: field.string + quotedGroupBy, // search on translated name and on technical name
};
}

Expand Down
20 changes: 12 additions & 8 deletions src/registries/auto_completes/pivot_auto_complete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
makeFieldProposal,
} from "../../helpers/pivot/pivot_helpers";
import { _t } from "../../translation";
import { Granularity } from "../../types";
import { autoCompleteProviders } from "./auto_complete_registry";

autoCompleteProviders.add("pivot_ids", {
Expand Down Expand Up @@ -119,7 +120,8 @@ autoCompleteProviders.add("pivot_group_fields", {
if (!fields) {
return;
}
const { columns, rows, type } = this.getters.getPivotCoreDefinition(pivotId);
const { type } = this.getters.getPivotCoreDefinition(pivotId);
const { columns, rows } = dataSource.definition;
if (!supportedPivotExplodedFormulaRegistry.get(type)) {
return [];
}
Expand All @@ -131,8 +133,8 @@ autoCompleteProviders.add("pivot_group_fields", {
args = args.filter((ast, index) => index % 2 === 1); // keep only the field names
}
const argGroupBys = args.map((ast) => ast?.value).filter(isDefined);
const colFields = columns.map((groupBy) => groupBy.name);
const rowFields = rows.map((groupBy) => groupBy.name);
const colFields = columns.map((groupBy) => groupBy.nameWithGranularity);
const rowFields = rows.map((groupBy) => groupBy.nameWithGranularity);

const proposals: string[] = [];
const previousGroupBy = ["ARG_SEPARATOR", "SPACE"].includes(tokenAtCursor.type)
Expand All @@ -154,16 +156,18 @@ autoCompleteProviders.add("pivot_group_fields", {
const groupBys = proposals.filter(isDefined);
return groupBys
.map((groupBy) => {
const field = fields[groupBy];
return field ? makeFieldProposal(field) : undefined;
const [fieldName, granularity] = groupBy.split(":");
const field = fields[fieldName];
return field ? makeFieldProposal(field, granularity as Granularity) : undefined;
})
.concat(
groupBys.map((groupBy) => {
const field = fields[groupBy];
const fieldName = groupBy.split(":")[0];
const field = fields[fieldName];
if (!field) {
return undefined;
}
const positionalFieldArg = `"#${field.name}"`;
const positionalFieldArg = `"#${groupBy}"`;
const positionalProposal = {
text: positionalFieldArg,
description:
Expand Down Expand Up @@ -229,7 +233,7 @@ autoCompleteProviders.add("pivot_group_values", {
if (!groupByField) {
return;
}
return dataSource.getPossibleFieldValues(groupByField).map(({ value, label }) => {
return dataSource.getPossibleFieldValues(groupByField.split(":")[0]).map(({ value, label }) => {
const isString = typeof value === "string";
const text = isString ? `"${value}"` : value.toString();
const color = isString ? tokenColors.STRING : tokenColors.NUMBER;
Expand Down

0 comments on commit c969fba

Please sign in to comment.