Skip to content

Commit

Permalink
[FIX] pivot: wrong headers with hidden measures
Browse files Browse the repository at this point in the history
The headers of the columns of the pivot were wrong if the pivot had
hidden measures. This was because we computed the column width for
the `SpreadsheetPivotTable `based on all the measures, not only
the non-hidden ones.

closes #5002

Task: 4190869
Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
  • Loading branch information
hokolomopo committed Sep 30, 2024
1 parent d85d4bc commit 70e8b8f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ export function dataEntriesToSpreadsheetPivotTable(
dataEntries: DataEntries,
definition: SpreadsheetPivotRuntimeDefinition
) {
const measureIds = definition.measures.filter((measure) => !measure.isHidden).map((m) => m.id);
const columnsTree = dataEntriesToColumnsTree(dataEntries, definition.columns, 0);
computeWidthOfColumnsNodes(columnsTree, definition.measures.length);
computeWidthOfColumnsNodes(columnsTree, measureIds.length);
const cols = columnsTreeToColumns(columnsTree, definition);

const rows = dataEntriesToRows(dataEntries, 0, definition.rows, [], []);
Expand All @@ -33,7 +34,6 @@ export function dataEntriesToSpreadsheetPivotTable(
indent: 0,
});

const measureIds = definition.measures.filter((measure) => !measure.isHidden).map((m) => m.id);
const fieldsType: Record<string, string> = {};
for (const columns of definition.columns) {
fieldsType[columns.fieldName] = columns.type;
Expand Down
25 changes: 25 additions & 0 deletions tests/pivots/spreadsheet_pivot/spreadsheet_pivot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2100,4 +2100,29 @@ describe("Spreadsheet arguments parsing", () => {
["Total", "", ""],
]);
});

test("Column headers are correct when hiding a measure", () => {
// prettier-ignore
const grid = {
A1: "Price", B1: "Tax", C1: "Salesman",
A2: "10", B2: "2", C2: "Alice",
A3: "20", B3: "4", C3: "Bob",
A5: "=PIVOT(1)",
};
const model = createModelFromGrid(grid);
addPivot(model, "A1:C3", {
rows: [],
columns: [{ fieldName: "Salesman" }],
measures: [
{ id: "Price:sum", fieldName: "Price", aggregator: "sum" },
{ id: "Tax:sum", fieldName: "Tax", aggregator: "sum", isHidden: true },
],
});
// prettier-ignore
expect(getEvaluatedGrid(model, "A5:D7")).toEqual([
["(#1) Pivot", "Alice", "Bob", "Total"],
["", "Price", "Price", "Price"],
["Total", "10", "20", "30"],
]);
});
});

0 comments on commit 70e8b8f

Please sign in to comment.