Skip to content

Commit

Permalink
[FIX] pivot: apply format on measure only a basic formula
Browse files Browse the repository at this point in the history
Let's say I have a computed measure "weighted_delay:sum", on which I applied
an integer format.

I later use it in a formula such as:

=PIVOT.VALUE(1,"weighted_delay:sum","#user_id",A3)/PIVOT.VALUE(1,"__count:sum","#user_id",A3)

This is no longer an integer. If I change the format on this formula ☝️, the
format is applied on the entire measure, which is not what I want.

With this commit, we apply the format on the measure (rather than on the cell)
if and only if the value comes from a spilled PIVOT formula.

closes #5299

Task: 4377411
Signed-off-by: Rémi Rahir (rar) <[email protected]>
  • Loading branch information
LucasLefevre committed Dec 12, 2024
1 parent 13a79d0 commit 8a18d36
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/plugins/ui_feature/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
CellValueType,
Command,
Format,
PivotTableCell,
PivotValueCell,
Position,
SetDecimalStep,
UID,
Expand Down Expand Up @@ -43,7 +45,7 @@ export class FormatPlugin extends UIPlugin {
for (let row = zone.top; row <= zone.bottom; row++) {
const position = { sheetId, col, row };
const pivotCell = this.getters.getPivotCellFromPosition(position);
if (pivotCell.type === "VALUE") {
if (this.isSpilledPivotValueFormula(position, pivotCell)) {
measurePositions.push(position);
const pivotId = this.getters.getPivotIdFromPosition(position) || "";
measuresByPivotId[pivotId] ??= new Set();
Expand Down Expand Up @@ -81,6 +83,13 @@ export class FormatPlugin extends UIPlugin {
});
}

private isSpilledPivotValueFormula(
position: CellPosition,
pivotCell: PivotTableCell
): pivotCell is PivotValueCell {
const cell = this.getters.getCell(position);
return pivotCell.type === "VALUE" && !cell?.isFormula;
}
/**
* This function allows to adjust the quantity of decimal places after a decimal
* point on cells containing number value. It does this by changing the cells
Expand Down
16 changes: 16 additions & 0 deletions tests/formats/formatting_plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,22 @@ describe("pivot contextual formatting", () => {
["Total", "$1.00"],
]);
});

test("format is not applied on the measure with fixed pivot values", () => {
// prettier-ignore
const grid = {
A1: "Customer", B1: "Price", C1: '=PIVOT.VALUE(1, "Price")',
A2: "Alice", B2: "10",
};
const model = createModelFromGrid(grid);
addPivot(model, "A1:B2", {
rows: [{ fieldName: "Customer" }],
measures: [{ id: "Price", fieldName: "Price", aggregator: "count" }],
});
setContextualFormat(model, "C1", "[$$]#,##0.00");
expect(model.getters.getPivotCoreDefinition("1")?.measures[0].format).toBeUndefined();
expect(getCell(model, "C1")?.format).toBe("[$$]#,##0.00");
});
});

describe("formatting values (when change decimal)", () => {
Expand Down

0 comments on commit 8a18d36

Please sign in to comment.