Skip to content

Commit e30e965

Browse files
committed
[IMP] pivots: implement pivot table styles
With this commit, we don't need to manually create a dynamic table to a pivot to have a style applied. Instead we can add a style in the pivot definition, and dynamic tables will automatically be created on the dynamic pivot formulas. Those new pivot styles are better than traditional tables styles because: - they are directly linked to the pivot, taking into account the number of headers, the presence of totals, etc. - they are automatically added on `=PIVOT()` formulas, without the need to create a dynamic table first. - they are more powerful than the old table styles, they can have a style for the sub-headers, the measure headers, etc. Task: 4552232
1 parent c1fb28a commit e30e965

File tree

51 files changed

+3274
-558
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+3274
-558
lines changed

packages/o-spreadsheet-engine/src/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export const NEWLINE = "\n";
250250
export const FONT_SIZES: number[] = [6, 7, 8, 9, 10, 11, 12, 14, 18, 24, 36];
251251

252252
// Pivot
253-
export const PIVOT_TABLE_CONFIG = {
253+
export const PIVOT_STATIC_TABLE_CONFIG = {
254254
hasFilters: false,
255255
totalRow: false,
256256
firstColumn: true,
@@ -261,6 +261,7 @@ export const PIVOT_TABLE_CONFIG = {
261261
styleId: "TableStyleMedium5",
262262
automaticAutofill: false,
263263
};
264+
export const PIVOT_INSERT_TABLE_STYLE_ID = "PivotTableStyleMedium12";
264265
export const PIVOT_INDENT = 15;
265266
export const PIVOT_COLLAPSE_ICON_SIZE = 12;
266267
export const PIVOT_MAX_NUMBER_OF_CELLS = 1e5;

packages/o-spreadsheet-engine/src/functions/module_lookup.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -957,15 +957,14 @@ export const PIVOT = {
957957
headerRows++;
958958
}
959959
const pivotTitle = this.getters.getPivotName(pivotId);
960-
const tableHeight = Math.min(headerRows + pivotStyle.numberOfRows, cells[0].length);
961-
if (tableHeight === 0) {
960+
const { numberOfCols, numberOfRows } = table.getPivotTableDimensions(pivotStyle);
961+
if (numberOfRows === 0) {
962962
return [[{ value: pivotTitle }]];
963963
}
964-
const tableWidth = Math.min(1 + pivotStyle.numberOfColumns, cells.length);
965964
const result: Matrix<FunctionResultObject> = [];
966-
for (const col of range(0, tableWidth)) {
965+
for (const col of range(0, numberOfCols)) {
967966
result[col] = [];
968-
for (const row of range(0, tableHeight)) {
967+
for (const row of range(0, numberOfRows)) {
969968
const pivotCell = cells[col][row];
970969
switch (pivotCell.type) {
971970
case "EMPTY":

packages/o-spreadsheet-engine/src/helpers/pivot/pivot_helpers.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ export const DEFAULT_PIVOT_STYLE: Required<PivotStyle> = {
4040
displayMeasuresRow: true,
4141
numberOfRows: Number.MAX_VALUE,
4242
numberOfColumns: Number.MAX_VALUE,
43+
tableStyleId: "None",
44+
bandedRows: false,
45+
bandedColumns: false,
46+
hasFilters: false,
4347
};
4448

4549
const AGGREGATOR_NAMES = {
@@ -489,5 +493,15 @@ export function getPivotStyleFromFnArgs(
489493
? toBoolean(includeMeasuresRowArg)
490494
: style?.displayMeasuresRow ?? DEFAULT_PIVOT_STYLE.displayMeasuresRow;
491495

492-
return { numberOfRows, numberOfColumns, displayTotals, displayColumnHeaders, displayMeasuresRow };
496+
return {
497+
numberOfRows,
498+
numberOfColumns,
499+
displayTotals,
500+
displayColumnHeaders,
501+
displayMeasuresRow,
502+
tableStyleId: style?.tableStyleId || DEFAULT_PIVOT_STYLE.tableStyleId,
503+
bandedRows: style?.bandedRows ?? DEFAULT_PIVOT_STYLE.bandedRows,
504+
bandedColumns: style?.bandedColumns ?? DEFAULT_PIVOT_STYLE.bandedColumns,
505+
hasFilters: style?.hasFilters ?? DEFAULT_PIVOT_STYLE.hasFilters,
506+
};
493507
}

packages/o-spreadsheet-engine/src/helpers/pivot/table_spreadsheet_pivot.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,27 @@ export class SpreadsheetPivotTable {
418418
}
419419
return this.rows.filter((row) => row.indent === depth + 1).map((row) => this.getDomain(row));
420420
}
421+
422+
getPivotTableDimensions(pivotStyle: Required<PivotStyle>) {
423+
const cells = this.getPivotCells(pivotStyle);
424+
let headerRows = 0;
425+
if (pivotStyle.displayColumnHeaders) {
426+
headerRows = this.columns.length - 1;
427+
}
428+
if (pivotStyle.displayMeasuresRow) {
429+
headerRows++;
430+
}
431+
432+
return {
433+
numberOfCols: Math.min(1 + pivotStyle.numberOfColumns, cells.length),
434+
numberOfRows: Math.min(headerRows + pivotStyle.numberOfRows, cells[0].length),
435+
headerRows,
436+
};
437+
}
438+
439+
getNumberOfRowGroupBys() {
440+
return Math.max(...this.rows.map((row) => row.fields.length));
441+
}
421442
}
422443

423444
export const EMPTY_PIVOT_CELL = { type: "EMPTY" } as const;

0 commit comments

Comments
 (0)