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 627e852531..1877432466 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)", () => {