diff --git a/src/plugins/ui_core_views/pivot_ui.ts b/src/plugins/ui_core_views/pivot_ui.ts index 47de1ab44f..6d513ce703 100644 --- a/src/plugins/ui_core_views/pivot_ui.ts +++ b/src/plugins/ui_core_views/pivot_ui.ts @@ -207,33 +207,37 @@ export class PivotUIPlugin extends UIPlugin { const pivotRow = position.row - mainPosition.row; return pivotCells[pivotCol][pivotRow]; } - if (functionName === "PIVOT.HEADER" && args.at(-2) === "measure") { - const domain = pivot.parseArgsToPivotDomain( - args.slice(1, -2).map((value) => ({ value } as FunctionResultObject)) - ); - return { - type: "MEASURE_HEADER", - domain, - measure: args.at(-1)?.toString() || "", - }; - } else if (functionName === "PIVOT.HEADER") { + try { + if (functionName === "PIVOT.HEADER" && args.at(-2) === "measure") { + const domain = pivot.parseArgsToPivotDomain( + args.slice(1, -2).map((value) => ({ value } as FunctionResultObject)) + ); + return { + type: "MEASURE_HEADER", + domain, + measure: args.at(-1)?.toString() || "", + }; + } else if (functionName === "PIVOT.HEADER") { + const domain = pivot.parseArgsToPivotDomain( + args.slice(1).map((value) => ({ value } as FunctionResultObject)) + ); + return { + type: "HEADER", + domain, + }; + } + const [measure, ...domainArgs] = args.slice(1); const domain = pivot.parseArgsToPivotDomain( - args.slice(1).map((value) => ({ value } as FunctionResultObject)) + domainArgs.map((value) => ({ value } as FunctionResultObject)) ); return { - type: "HEADER", + type: "VALUE", domain, + measure: measure?.toString() || "", }; + } catch (_) { + return EMPTY_PIVOT_CELL; } - const [measure, ...domainArgs] = args.slice(1); - const domain = pivot.parseArgsToPivotDomain( - domainArgs.map((value) => ({ value } as FunctionResultObject)) - ); - return { - type: "VALUE", - domain, - measure: measure?.toString() || "", - }; } getPivot(pivotId: UID) { diff --git a/tests/pivots/pivot_plugin.test.ts b/tests/pivots/pivot_plugin.test.ts index 57e9679df0..02699c724e 100644 --- a/tests/pivots/pivot_plugin.test.ts +++ b/tests/pivots/pivot_plugin.test.ts @@ -1,4 +1,5 @@ -import { setCellContent } from "../test_helpers/commands_helpers"; +import { EMPTY_PIVOT_CELL } from "../../src/helpers/pivot/table_spreadsheet_pivot"; +import { selectCell, setCellContent } from "../test_helpers/commands_helpers"; import { createModelFromGrid, toCellPosition } from "../test_helpers/helpers"; import { addPivot } from "../test_helpers/pivot_helpers"; @@ -28,4 +29,23 @@ describe("Pivot plugin", () => { setCellContent(model, "G1", "=PIVOT.HEADER(1)"); expect(isSpillPivotFormula("G1")).toBe(false); }); + + test("getPivotCellFromPosition doesn't throw with invalid pivot domain", () => { + // prettier-ignore + const grid = { + A1: "Customer", B1: "Price", C1: '=PIVOT.VALUE(1,"Price","5","Bob")', + A2: "Alice", B2: "10", + A3: "Bob", B3: "30", + }; + const model = createModelFromGrid(grid); + addPivot(model, "A1:B3", { + columns: [], + rows: [{ fieldName: "Customer" }], + measures: [{ id: "Price:sum", fieldName: "Price", aggregator: "sum" }], + }); + selectCell(model, "C1"); + expect(model.getters.getPivotCellFromPosition(model.getters.getActivePosition())).toMatchObject( + EMPTY_PIVOT_CELL + ); + }); });