From de260ca2240db82d7dcdef6e74b5507ba37238ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Rahir=20=28rar=29?= Date: Tue, 24 Sep 2024 12:45:40 +0000 Subject: [PATCH] [FIX] pivot: handle boolean-like function arguments in `getPivotCellFromPosition` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The getter would only detect the 'includeTotal' and 'includeColumnHeaders' arguments if they were explicitely written as boolean and not as boolean-like (i.e; 1,2, 0, -1) values. closes odoo/o-spreadsheet#5290 Task: 4207502 X-original-commit: 0943356581fb32328014574ed1c3e4eca175588e Signed-off-by: Lucas Lefèvre (lul) Signed-off-by: Rémi Rahir (rar) --- src/plugins/ui_core_views/pivot_ui.ts | 11 +++++-- tests/pivots/pivot_plugin.test.ts | 43 +++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/plugins/ui_core_views/pivot_ui.ts b/src/plugins/ui_core_views/pivot_ui.ts index 00b1e9255a..bb33798940 100644 --- a/src/plugins/ui_core_views/pivot_ui.ts +++ b/src/plugins/ui_core_views/pivot_ui.ts @@ -1,5 +1,7 @@ import { Token } from "../../formulas"; import { astToFormula } from "../../formulas/parser"; +import { toScalar } from "../../functions/helper_matrices"; +import { toBoolean } from "../../functions/helpers"; import { getFirstPivotFunction, getNumberOfPivotFunctions, @@ -216,11 +218,14 @@ export class PivotUIPlugin extends UIPlugin { return EMPTY_PIVOT_CELL; } if (functionName === "PIVOT") { - const includeTotal = args[2] === false ? false : undefined; - const includeColumnHeaders = args[3] === false ? false : undefined; + const includeTotal = toScalar(args[2]); + const shouldIncludeTotal = includeTotal === undefined ? true : toBoolean(includeTotal); + const includeColumnHeaders = toScalar(args[3]); + const shouldIncludeColumnHeaders = + includeColumnHeaders === undefined ? true : toBoolean(includeColumnHeaders); const pivotCells = pivot .getTableStructure() - .getPivotCells(includeTotal, includeColumnHeaders); + .getPivotCells(shouldIncludeTotal, shouldIncludeColumnHeaders); const pivotCol = position.col - mainPosition.col; const pivotRow = position.row - mainPosition.row; return pivotCells[pivotCol][pivotRow]; diff --git a/tests/pivots/pivot_plugin.test.ts b/tests/pivots/pivot_plugin.test.ts index 36fdad5c2c..bca9433700 100644 --- a/tests/pivots/pivot_plugin.test.ts +++ b/tests/pivots/pivot_plugin.test.ts @@ -220,4 +220,47 @@ describe("Pivot plugin", () => { }); expect(model.getters.getSheetName("Sheet2")).toEqual("forbidden: (copy) (Pivot #2) (1)"); }); + + test("getPivotCellFromPosition handles falsy arguments for includeColumnTitle", () => { + // prettier-ignore + const grid = { + A1: "Customer", B1: "Price", + A2: "Alice", B2: "10", + A3: "Bob", B3: "30", + }; + const model = createModelFromGrid(grid); + addPivot(model, "A1:B3", { + columns: [{ fieldName: "Customer" }], + rows: [{ fieldName: "Price" }], + measures: [{ id: "testCount", fieldName: "__count", aggregator: "sum" }], + }); + const sheetId = model.getters.getActiveSheetId(); + setCellContent(model, "C1", "=PIVOT(1,,,false)"); + expect(model.getters.getPivotCellFromPosition(toCellPosition(sheetId, "D1"))).toMatchObject({ + measure: "testCount", + type: "VALUE", + }); + setCellContent(model, "C1", "=PIVOT(1,,,0)"); + expect(model.getters.getPivotCellFromPosition(toCellPosition(sheetId, "D1"))).toMatchObject({ + measure: "testCount", + type: "VALUE", + }); + setCellContent(model, "C1", `=PIVOT(1,,,"FALSE")`); + expect(model.getters.getPivotCellFromPosition(toCellPosition(sheetId, "D1"))).toMatchObject({ + measure: "testCount", + type: "VALUE", + }); + setCellContent(model, "C1", "=PIVOT(1,,,true)"); + expect(model.getters.getPivotCellFromPosition(toCellPosition(sheetId, "D1"))).toMatchObject({ + type: "HEADER", + }); + setCellContent(model, "C1", "=PIVOT(1,,,1)"); + expect(model.getters.getPivotCellFromPosition(toCellPosition(sheetId, "D1"))).toMatchObject({ + type: "HEADER", + }); + setCellContent(model, "C1", `=PIVOT(1,,,"TRUE")`); + expect(model.getters.getPivotCellFromPosition(toCellPosition(sheetId, "D1"))).toMatchObject({ + type: "HEADER", + }); + }); });