From 171a0c737f01ec4b8c98e935b38d73e3b73ec6fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Lef=C3=A8vre=20=28lul=29?= Date: Wed, 8 Jan 2025 14:13:55 +0100 Subject: [PATCH] [FIX] pivot: notify only with static formulas When we update a pivot from it's side panel with a static pivot present in the viewport, we have the warning "Pivot updates only work with dynamic pivot tables". But the warning is also present if you have no static pivot anywhere, but that there is no dynamic pivot in the viewport (eg. you scolled a bit after opening the pivot side panel). The warning should only be displayed if you have a static pivot somewhere in the viewport and no dynamic pivot. Task: 4453844 --- .../pivot_side_panel_store.ts | 20 ++++++++++++++++++- .../spreadsheet_pivot_side_panel.test.ts | 6 ++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/components/side_panel/pivot/pivot_side_panel/pivot_side_panel_store.ts b/src/components/side_panel/pivot/pivot_side_panel/pivot_side_panel_store.ts index 5c6e7db547..883a942058 100644 --- a/src/components/side_panel/pivot/pivot_side_panel/pivot_side_panel_store.ts +++ b/src/components/side_panel/pivot/pivot_side_panel/pivot_side_panel_store.ts @@ -1,4 +1,5 @@ import { deepCopy, deepEquals } from "../../../../helpers"; +import { getFirstPivotFunction } from "../../../../helpers/pivot/pivot_composer_helpers"; import { isDateOrDatetimeField } from "../../../../helpers/pivot/pivot_helpers"; import { pivotRegistry } from "../../../../helpers/pivot/pivot_registry"; import { Get } from "../../../../store_engine"; @@ -140,7 +141,11 @@ export class PivotSidePanelStore extends SpreadsheetStore { pivot: this.draft, }); this.draft = null; - if (!this.alreadyNotified && !this.isDynamicPivotInViewport()) { + if ( + !this.alreadyNotified && + !this.isDynamicPivotInViewport() && + this.isStaticPivotInViewport() + ) { const formulaId = this.getters.getPivotFormulaId(this.pivotId); const pivotExample = `=PIVOT(${formulaId})`; this.alreadyNotified = true; @@ -217,6 +222,19 @@ export class PivotSidePanelStore extends SpreadsheetStore { return false; } + private isStaticPivotInViewport() { + for (const position of this.getters.getVisibleCellPositions()) { + const cell = this.getters.getCell(position); + if (cell?.isFormula) { + const pivotFunction = getFirstPivotFunction(cell.compiledFormula.tokens); + if (pivotFunction && pivotFunction.functionName !== "PIVOT") { + return true; + } + } + } + return false; + } + private addDefaultDateTimeGranularity(fields: PivotFields, definition: PivotCoreDefinition) { const { columns, rows } = definition; const columnsWithGranularity = deepCopy(columns); diff --git a/tests/pivots/spreadsheet_pivot/spreadsheet_pivot_side_panel.test.ts b/tests/pivots/spreadsheet_pivot/spreadsheet_pivot_side_panel.test.ts index 5e827be40b..a4aab66696 100644 --- a/tests/pivots/spreadsheet_pivot/spreadsheet_pivot_side_panel.test.ts +++ b/tests/pivots/spreadsheet_pivot/spreadsheet_pivot_side_panel.test.ts @@ -472,6 +472,12 @@ describe("Spreadsheet pivot side panel", () => { setViewportOffset(model, 0, 1000); await click(fixture.querySelector(".o-pivot-measure .add-dimension")!); await click(fixture.querySelectorAll(".o-autocomplete-value")[1]); + expect(mockNotify).toHaveBeenCalledTimes(0); + + // add a static pivot in the viewport + setCellContent(model, "A50", "=PIVOT.VALUE(1)"); + await click(fixture.querySelector(".o-pivot-measure .add-dimension")!); + await click(fixture.querySelectorAll(".o-autocomplete-value")[1]); expect(mockNotify).toHaveBeenCalledWith({ text: "Pivot updates only work with dynamic pivot tables. Use =PIVOT(1) or re-insert the static pivot from the Data menu.", sticky: false,