Skip to content

Commit

Permalink
[FIX] pivot: handle boolean-like function arguments in `getPivotCellF…
Browse files Browse the repository at this point in the history
…romPosition`

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 #5290

Task: 4207502
X-original-commit: 0943356
Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
Signed-off-by: Rémi Rahir (rar) <[email protected]>
  • Loading branch information
rrahir committed Dec 2, 2024
1 parent 02b6618 commit de260ca
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/plugins/ui_core_views/pivot_ui.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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];
Expand Down
43 changes: 43 additions & 0 deletions tests/pivots/pivot_plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
});
});

0 comments on commit de260ca

Please sign in to comment.