Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/o-spreadsheet-engine/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ export const DEFAULT_STYLE = {
fillColor: "",
textColor: "",
rotation: 0,
hideGridLines: false,
} satisfies Required<Style>;

export const DEFAULT_VERTICAL_ALIGN = DEFAULT_STYLE.verticalAlign;
Expand Down Expand Up @@ -253,7 +254,7 @@ export const NEWLINE = "\n";
export const FONT_SIZES: number[] = [6, 7, 8, 9, 10, 11, 12, 14, 18, 24, 36];

// Pivot
export const PIVOT_TABLE_CONFIG = {
export const PIVOT_STATIC_TABLE_CONFIG = {
hasFilters: false,
totalRow: false,
firstColumn: true,
Expand All @@ -264,6 +265,7 @@ export const PIVOT_TABLE_CONFIG = {
styleId: "TableStyleMedium5",
automaticAutofill: false,
};
export const PIVOT_INSERT_TABLE_STYLE_ID = "PivotTableStyleMedium12";
export const PIVOT_INDENT = 15;
export const PIVOT_COLLAPSE_ICON_SIZE = 12;
export const PIVOT_MAX_NUMBER_OF_CELLS = 5e5;
Expand Down
9 changes: 4 additions & 5 deletions packages/o-spreadsheet-engine/src/functions/module_lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -957,15 +957,14 @@ export const PIVOT = {
headerRows++;
}
const pivotTitle = this.getters.getPivotName(pivotId);
const tableHeight = Math.min(headerRows + pivotStyle.numberOfRows, cells[0].length);
if (tableHeight === 0) {
const { numberOfCols, numberOfRows } = table.getPivotTableDimensions(pivotStyle);
if (numberOfRows === 0) {
return [[{ value: pivotTitle }]];
}
const tableWidth = Math.min(1 + pivotStyle.numberOfColumns, cells.length);
const result: Matrix<FunctionResultObject> = [];
for (const col of range(0, tableWidth)) {
for (const col of range(0, numberOfCols)) {
result[col] = [];
for (const row of range(0, tableHeight)) {
for (const row of range(0, numberOfRows)) {
const pivotCell = cells[col][row];
switch (pivotCell.type) {
case "EMPTY":
Expand Down
5 changes: 2 additions & 3 deletions packages/o-spreadsheet-engine/src/functions/module_math.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { splitReference } from "../helpers";
import { doesCellContainFormula, splitReference } from "../helpers";
import { toZone } from "../helpers/zones";
import { isSubtotalCell } from "../plugins/ui_feature/subtotal_evaluation";
import { _t } from "../translation";
import { EvaluatedCell } from "../types/cells";
import { DivisionByZeroError, EvaluationError } from "../types/errors";
Expand Down Expand Up @@ -1410,7 +1409,7 @@ export const SUBTOTAL = {

for (let col = left; col <= right; col++) {
const cell = this.getters.getCell({ sheetId, col, row });
if (!cell || !isSubtotalCell(cell)) {
if (!cell || !doesCellContainFormula(cell, "SUBTOTAL")) {
evaluatedCellToKeep.push(this.getters.getEvaluatedCell({ sheetId, col, row }));
}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/o-spreadsheet-engine/src/helpers/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Miscellaneous
//------------------------------------------------------------------------------
import { FORBIDDEN_SHEETNAME_CHARS_IN_EXCEL_REGEX, NEWLINE } from "../constants";
import { Cell } from "../types/cells";
import { ChartStyle } from "../types/chart";
import { SearchOptions } from "../types/find_and_replace";
import { Cloneable, ConsecutiveIndexes, DebouncedFunction, Lazy, Style, UID } from "../types/misc";
Expand Down Expand Up @@ -717,3 +718,12 @@ export function chartStyleToCellStyle(style: ChartStyle): Style {
align: style.align,
};
}

export function doesCellContainFormula(cell: Cell, formula: string): boolean {
return (
cell.isFormula &&
cell.compiledFormula.tokens.some(
(t) => t.type === "SYMBOL" && t.value.toUpperCase() === formula
)
);
}
16 changes: 15 additions & 1 deletion packages/o-spreadsheet-engine/src/helpers/pivot/pivot_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export const DEFAULT_PIVOT_STYLE: Required<PivotStyle> = {
displayMeasuresRow: true,
numberOfRows: Number.MAX_VALUE,
numberOfColumns: Number.MAX_VALUE,
tableStyleId: "None",
bandedRows: false,
bandedColumns: false,
hasFilters: false,
};

const AGGREGATOR_NAMES = {
Expand Down Expand Up @@ -489,5 +493,15 @@ export function getPivotStyleFromFnArgs(
? toBoolean(includeMeasuresRowArg)
: style?.displayMeasuresRow ?? DEFAULT_PIVOT_STYLE.displayMeasuresRow;

return { numberOfRows, numberOfColumns, displayTotals, displayColumnHeaders, displayMeasuresRow };
return {
numberOfRows,
numberOfColumns,
displayTotals,
displayColumnHeaders,
displayMeasuresRow,
tableStyleId: style?.tableStyleId || DEFAULT_PIVOT_STYLE.tableStyleId,
bandedRows: style?.bandedRows ?? DEFAULT_PIVOT_STYLE.bandedRows,
bandedColumns: style?.bandedColumns ?? DEFAULT_PIVOT_STYLE.bandedColumns,
hasFilters: style?.hasFilters ?? DEFAULT_PIVOT_STYLE.hasFilters,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,27 @@ export class SpreadsheetPivotTable {
}
return this.rows.filter((row) => row.indent === depth + 1).map((row) => this.getDomain(row));
}

getPivotTableDimensions(pivotStyle: Required<PivotStyle>) {
const cells = this.getPivotCells(pivotStyle);
let headerRows = 0;
if (pivotStyle.displayColumnHeaders) {
headerRows = this.columns.length - 1;
}
if (pivotStyle.displayMeasuresRow) {
headerRows++;
}

return {
numberOfCols: Math.min(1 + pivotStyle.numberOfColumns, cells.length),
numberOfRows: Math.min(headerRows + pivotStyle.numberOfRows, cells[0].length),
headerRows,
};
}

getNumberOfRowGroupBys() {
return Math.max(...this.rows.map((row) => row.fields.length));
}
}

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