Skip to content

Commit

Permalink
[FIX] pivot: ensure correct format status in menu
Browse files Browse the repository at this point in the history
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.

Task: 4373606
  • Loading branch information
dhrp-odoo committed Dec 11, 2024
1 parent 13a79d0 commit 07fdc17
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/actions/format_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 19 additions & 1 deletion tests/formats/formatting_plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -371,6 +371,24 @@ describe("pivot contextual formatting", () => {
["Total", "$1.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)", () => {
Expand Down

0 comments on commit 07fdc17

Please sign in to comment.