From d80d182228b443a4f494cda2721bde8aefc39aa2 Mon Sep 17 00:00:00 2001 From: "Adrien Minne (adrm)" Date: Wed, 18 Sep 2024 12:53:01 +0200 Subject: [PATCH] [IMP] pivot: correctly align column header MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The pivot column headers were aligned all over the place if the header was a boolean or a number. This commit forces the alignment to be to the left with the "* " format. closes odoo/o-spreadsheet#4999 Task: 4193485 Signed-off-by: Lucas Lefèvre (lul) --- src/functions/module_lookup.ts | 6 ++++-- src/helpers/pivot/pivot_helpers.ts | 14 +++++++++++--- .../spreadsheet_pivot.test.ts | 19 +++++++++++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/functions/module_lookup.ts b/src/functions/module_lookup.ts index e2d41ec01d..03c453efba 100644 --- a/src/functions/module_lookup.ts +++ b/src/functions/module_lookup.ts @@ -1,5 +1,5 @@ import { getFullReference, range, splitReference, toXC, toZone } from "../helpers/index"; -import { addRowIndentToPivotHeader } from "../helpers/pivot/pivot_helpers"; +import { addIndentAndAlignToPivotHeader } from "../helpers/pivot/pivot_helpers"; import { _t } from "../translation"; import { AddFunctionDescription, FunctionResultObject, Matrix, Maybe, Zone } from "../types"; import { CellErrorType, EvaluationError, InvalidReferenceError } from "../types/errors"; @@ -831,7 +831,9 @@ export const PIVOT = { break; case "HEADER": const valueAndFormat = pivot.getPivotHeaderValueAndFormat(pivotCell.domain); - result[col].push(addRowIndentToPivotHeader(pivot, pivotCell.domain, valueAndFormat)); + result[col].push( + addIndentAndAlignToPivotHeader(pivot, pivotCell.domain, valueAndFormat) + ); break; case "MEASURE_HEADER": result[col].push(pivot.getPivotMeasureValue(pivotCell.measure, pivotCell.domain)); diff --git a/src/helpers/pivot/pivot_helpers.ts b/src/helpers/pivot/pivot_helpers.ts index 1ca7e4cadd..5000cb0f6e 100644 --- a/src/helpers/pivot/pivot_helpers.ts +++ b/src/helpers/pivot/pivot_helpers.ts @@ -274,15 +274,23 @@ export function getFieldDisplayName(field: PivotDimension) { return field.displayName + (field.granularity ? ` (${ALL_PERIODS[field.granularity]})` : ""); } -export function addRowIndentToPivotHeader( +export function addIndentAndAlignToPivotHeader( pivot: Pivot, domain: PivotDomain, functionResult: FunctionResultObject ): FunctionResultObject { - const { rowDomain } = domainToColRowDomain(pivot, domain); - if (rowDomain.length === 0) { + const { rowDomain, colDomain } = domainToColRowDomain(pivot, domain); + if (rowDomain.length === 0 && colDomain.length === 0) { return functionResult; } + + if (rowDomain.length === 0 && colDomain.length > 0) { + return { + ...functionResult, + format: (functionResult.format || "@") + "* ", + }; + } + const indent = rowDomain.length - 1; const format = functionResult.format || "@"; diff --git a/tests/pivots/spreadsheet_pivot/spreadsheet_pivot.test.ts b/tests/pivots/spreadsheet_pivot/spreadsheet_pivot.test.ts index e81399b633..dd689c2f4c 100644 --- a/tests/pivots/spreadsheet_pivot/spreadsheet_pivot.test.ts +++ b/tests/pivots/spreadsheet_pivot/spreadsheet_pivot.test.ts @@ -729,6 +729,25 @@ describe("Spreadsheet Pivot", () => { ]); }); + test("Pivot column headers are aligned left with their format", () => { + // prettier-ignore + const grid = { + A1: "Date", B1: "Price", C1: "Active", D1: "=PIVOT(1)", + A2: "2024-12-28", B2: "10", C2: "TRUE", + A3: "2024-12-29", B3: "20", C3: "FALSE", + }; + const model = createModelFromGrid(grid); + addPivot(model, "A1:C3", { + rows: [], + columns: [{ fieldName: "Date", granularity: "year" }, { fieldName: "Active" }], + measures: [{ id: "Price:sum", fieldName: "Price", aggregator: "sum" }], + }); + + expect(getEvaluatedCell(model, "E1")).toMatchObject({ value: 2024, format: "0* " }); + expect(getEvaluatedCell(model, "E2")).toMatchObject({ value: "TRUE", format: "@* " }); + expect(getEvaluatedCell(model, "F2")).toMatchObject({ value: "FALSE", format: "@* " }); + }); + test("PIVOT.HEADER grand total", () => { const model = createModelWithPivot("A1:I5"); updatePivot(model, "1", {