From 4cf2a608d0d9ca752e9c4a2956257da05d3cd1fd Mon Sep 17 00:00:00 2001 From: dhrp-odoo Date: Sat, 7 Dec 2024 14:10:14 +0000 Subject: [PATCH] [FIX] pivot: ensure correct format status in menu MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since PR#4657, users can define a custom format for pivot measures by selecting a cell with a measure value and applying a format. However, the format menu's tick status incorrectly displayed "Automatic" even after applying a custom format. This was caused by the getCell method fetching the cell format, which does not account for pivot measure-specific formats. This fix ensures the format menu reflects the correct status by properly handling pivot cell measure formats. closes odoo/o-spreadsheet#5418 Task: 4373606 X-original-commit: 226fa6ad5998735898075c7b7f4c32c562078c51 Signed-off-by: RĂ©mi Rahir (rar) Signed-off-by: Dhrutik Patel (dhrp) --- src/actions/format_actions.ts | 16 ++++++++++++---- tests/formats/formatting_plugin.test.ts | 20 +++++++++++++++++++- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/actions/format_actions.ts b/src/actions/format_actions.ts index e4f28ff933..fc77cbc853 100644 --- a/src/actions/format_actions.ts +++ b/src/actions/format_actions.ts @@ -396,13 +396,21 @@ function fontSizeMenuBuilder(): ActionSpec[] { } function isAutomaticFormatSelected(env: SpreadsheetChildEnv): boolean { - const activeCell = env.model.getters.getCell(env.model.getters.getActivePosition()); - return !activeCell || !activeCell.format; + const activePosition = env.model.getters.getActivePosition(); + const pivotCell = env.model.getters.getPivotCellFromPosition(activePosition); + if (pivotCell.type === "VALUE") { + return !env.model.getters.getEvaluatedCell(activePosition).format; + } + return !env.model.getters.getCell(activePosition)?.format; } function isFormatSelected(env: SpreadsheetChildEnv, format: string): boolean { - const activeCell = env.model.getters.getCell(env.model.getters.getActivePosition()); - return activeCell?.format === format; + const activePosition = env.model.getters.getActivePosition(); + const pivotCell = env.model.getters.getPivotCellFromPosition(activePosition); + if (pivotCell.type === "VALUE") { + return env.model.getters.getEvaluatedCell(activePosition).format === format; + } + return env.model.getters.getCell(activePosition)?.format === format; } function isFontSizeSelected(env: SpreadsheetChildEnv, fontSize: number): boolean { diff --git a/tests/formats/formatting_plugin.test.ts b/tests/formats/formatting_plugin.test.ts index f5f51c2964..279bf016d8 100644 --- a/tests/formats/formatting_plugin.test.ts +++ b/tests/formats/formatting_plugin.test.ts @@ -31,7 +31,7 @@ import { getEvaluatedCell, getEvaluatedGrid, } from "../test_helpers/getters_helpers"; -import { createModelFromGrid, target } from "../test_helpers/helpers"; +import { createModelFromGrid, getNode, makeTestEnv, target } from "../test_helpers/helpers"; import { addPivot } from "../test_helpers/pivot_helpers"; function setDecimal(model: Model, targetXc: string, step: SetDecimalStep) { @@ -387,6 +387,24 @@ describe("pivot contextual formatting", () => { expect(model.getters.getPivotCoreDefinition("1")?.measures[0].format).toBeUndefined(); expect(getCell(model, "C1")?.format).toBe("[$$]#,##0.00"); }); + + test("topbar menu correctly indicates the format of the selected pivot cell", () => { + const env = makeTestEnv(); + const { model } = env; + + setCellContent(model, "A1", "Price"); + setCellContent(model, "A2", "10"); + setCellContent(model, "B1", "=PIVOT(1)"); + + addPivot(model, "A1:A2", { + measures: [{ id: "Price", fieldName: "Price", aggregator: "sum" }], + }); + setContextualFormat(model, "C3", "[$$]#,##0.00"); + selectCell(model, "C3"); + + const action = getNode(["format", "format_number", "format_number_currency"], env); + expect(action.isActive?.(env)).toBe(true); + }); }); describe("formatting values (when change decimal)", () => {