Skip to content

Commit 500396c

Browse files
committed
[FIX] pivot: support sorted column with field name
Fix for an upgrade issue: the sortedColumn measure was not updated from using fieldName to using id. If the sortedColumn measure matches a measure fieldName in the definition, update it to use the measure's id instead of its fieldName. closes #6533 Task: 4816705 X-original-commit: c737e58 Signed-off-by: Pierre Rousseau (pro) <[email protected]>
1 parent e379ca8 commit 500396c

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

src/plugins/core/pivot.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,12 @@ export class PivotCorePlugin extends CorePlugin<CoreState> implements CoreState
124124
break;
125125
}
126126
case "UPDATE_PIVOT": {
127-
this.history.update("pivots", cmd.pivotId, "definition", deepCopy(cmd.pivot));
127+
this.history.update(
128+
"pivots",
129+
cmd.pivotId,
130+
"definition",
131+
this.repairSortedColumn(deepCopy(cmd.pivot))
132+
);
128133
this.compileCalculatedMeasures(cmd.pivot.measures);
129134
break;
130135
}
@@ -214,7 +219,10 @@ export class PivotCorePlugin extends CorePlugin<CoreState> implements CoreState
214219
pivot: PivotCoreDefinition,
215220
formulaId = this.nextFormulaId.toString()
216221
) {
217-
this.history.update("pivots", pivotId, { definition: deepCopy(pivot), formulaId });
222+
this.history.update("pivots", pivotId, {
223+
definition: this.repairSortedColumn(deepCopy(pivot)),
224+
formulaId,
225+
});
218226
this.compileCalculatedMeasures(pivot.measures);
219227
this.history.update("formulaIds", formulaId, pivotId);
220228
this.history.update("nextFormulaId", this.nextFormulaId + 1);
@@ -339,6 +347,29 @@ export class PivotCorePlugin extends CorePlugin<CoreState> implements CoreState
339347
return CommandResult.Success;
340348
}
341349

350+
private repairSortedColumn(definition: PivotCoreDefinition) {
351+
if (definition.sortedColumn) {
352+
// Fix for an upgrade issue: the sortedColumn measure was not updated
353+
// from using fieldName to using id. If the sortedColumn measure matches
354+
// a measure fieldName in the definition, update it to use the measure's id instead
355+
// of its fieldName.
356+
// TODO: add an upgrade step to fix this in master and remove this code
357+
const sortedMeasure = definition.measures.find(
358+
(measure) => measure.fieldName === definition.sortedColumn?.measure
359+
);
360+
if (sortedMeasure) {
361+
return {
362+
...definition,
363+
sortedColumn: {
364+
...definition.sortedColumn,
365+
measure: sortedMeasure.id,
366+
},
367+
};
368+
}
369+
}
370+
return definition;
371+
}
372+
342373
// ---------------------------------------------------------------------
343374
// Import/Export
344375
// ---------------------------------------------------------------------

tests/pivots/pivot_sorting.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,49 @@ describe("Pivot sorting", () => {
6767
});
6868
});
6969

70+
test("sortedColumn.measure can be a field name instead of the measure id", () => {
71+
// prettier-ignore
72+
const grid = {
73+
A1: "Customer", B1: "Price",
74+
A2: "Alice", B2: "10",
75+
A3: "Bob", B3: "20",
76+
A4: "=PIVOT(1)"
77+
};
78+
const model = createModelFromGrid(grid);
79+
const sortedColumn: PivotSortedColumn = {
80+
domain: [],
81+
order: "desc",
82+
measure: "Price", // should be "Price:sum" instead of "Price", but we accept it
83+
};
84+
addPivot(model, "A1:B3", {
85+
columns: [],
86+
rows: [{ fieldName: "Customer" }],
87+
measures: [{ id: "Price:sum", fieldName: "Price", aggregator: "sum" }],
88+
sortedColumn,
89+
});
90+
91+
const pivotId = model.getters.getPivotIds()[0];
92+
expect(model.getters.getPivot(pivotId).getTableStructure().isSorted).toBe(true);
93+
// prettier-ignore
94+
expect(getGrid(model)).toMatchObject({
95+
A4: "(#1) Pivot", B4: "Total",
96+
A5: "", B5: "Price",
97+
A6: "Bob", B6: 20,
98+
A7: "Alice", B7: 10,
99+
A8: "Total", B8: 30,
100+
});
101+
updatePivot(model, pivotId, { sortedColumn: { ...sortedColumn, order: "asc" } });
102+
expect(model.getters.getPivot(pivotId).getTableStructure().isSorted).toBe(true);
103+
// prettier-ignore
104+
expect(getGrid(model)).toMatchObject({
105+
A4: "(#1) Pivot", B4: "Total",
106+
A5: "", B5: "Price",
107+
A6: "Alice", B6: 10,
108+
A7: "Bob", B7: 20,
109+
A8: "Total", B8: 30,
110+
});
111+
});
112+
70113
test("Empty values are sorted as the smallest value", () => {
71114
const model = createModelWithTestPivotDataset();
72115
const bobColumn: PivotSortedColumn = {

0 commit comments

Comments
 (0)