From 36b50ff5846354d476f57f221fcc1add38a9b45f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Rahir=20=28rar=29?= Date: Tue, 24 Sep 2024 14:45:40 +0200 Subject: [PATCH] [FIX] pivot: handle boolean-like function arguments in `getPivotCellFromPosition` 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. Task: 4207502 --- 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 f7ca06e70b..7d9ebde627 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, @@ -204,11 +206,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 6eee6d1a79..a36c34257b 100644 --- a/tests/pivots/pivot_plugin.test.ts +++ b/tests/pivots/pivot_plugin.test.ts @@ -105,4 +105,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: [{ name: "Customer" }], + rows: [{ name: "Price" }], + measures: [{ name: "__count", aggregator: "sum" }], + }); + const sheetId = model.getters.getActiveSheetId(); + setCellContent(model, "C1", "=PIVOT(1,,,false)"); + expect(model.getters.getPivotCellFromPosition(toCellPosition(sheetId, "D1"))).toMatchObject({ + measure: "__count", + type: "VALUE", + }); + setCellContent(model, "C1", "=PIVOT(1,,,0)"); + expect(model.getters.getPivotCellFromPosition(toCellPosition(sheetId, "D1"))).toMatchObject({ + measure: "__count", + type: "VALUE", + }); + setCellContent(model, "C1", `=PIVOT(1,,,"FALSE")`); + expect(model.getters.getPivotCellFromPosition(toCellPosition(sheetId, "D1"))).toMatchObject({ + measure: "__count", + 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", + }); + }); });