Skip to content
Closed
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
121 changes: 119 additions & 2 deletions packages/o-spreadsheet-engine/src/helpers/pivot/pivot_presentation.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { PivotParams, PivotUIConstructor } from "./pivot_registry";

import { handleError } from "../../functions/create_compute_function";
import { transposeMatrix } from "../../functions/helpers";
import { toNumber, transposeMatrix } from "../../functions/helpers";
import { _t } from "../../translation";
import { CellValue } from "../../types/cells";
import { CellErrorType, NotAvailableError } from "../../types/errors";
import { Getters } from "../../types/getters";
import { DEFAULT_LOCALE } from "../../types/locale";
import { FunctionResultObject, isMatrix, SortDirection, UID } from "../../types/misc";
import { ModelConfig } from "../../types/model";
import {
DimensionTree,
Granularity,
NEXT_VALUE,
PivotDimension,
PivotDomain,
PivotMeasure,
PivotMeasureDisplay,
Expand All @@ -34,6 +37,7 @@ import {
replaceFieldValueInDomain,
} from "./pivot_domain_helpers";
import { AGGREGATORS_FN, isSortedColumnValid, toNormalizedPivotValue } from "./pivot_helpers";
import { pivotTimeAdapter, pivotTimeAdapterRegistry } from "./pivot_time_adapter";
import { SpreadsheetPivotTable } from "./table_spreadsheet_pivot";

const PERCENT_FORMAT = "0.00%";
Expand Down Expand Up @@ -420,7 +424,14 @@ export default function (PivotClass: PivotUIConstructor) {
const { rowDomain, colDomain } = domainToColRowDomain(this, domain);
const colDomainKey = domainToString(colDomain);
const rowDomainKey = domainToString(rowDomain);
const runningTotal = runningTotals[colDomainKey]?.[rowDomainKey];
let runningTotal = runningTotals[colDomainKey]?.[rowDomainKey];
if (runningTotal === undefined) {
runningTotal = this.getPreviousRunningTotalValue(
fieldNameWithGranularity,
domain,
runningTotals
);
}

return {
value: runningTotal ?? "",
Expand Down Expand Up @@ -679,6 +690,112 @@ export default function (PivotClass: PivotUIConstructor) {
return mainDimension === "row" ? cellsRunningTotals : transpose2dPOJO(cellsRunningTotals);
}

private getPreviousRunningTotalValue(
fieldNameWithGranularity: string,
domain: PivotDomain,
runningTotals: DomainGroups<number | undefined>
): number | undefined {
const dimension = this.definition.getDimension(fieldNameWithGranularity);
if (dimension.type !== "date" && dimension.type !== "datetime") {
return undefined;
}

const mainDimension = getFieldDimensionType(this, fieldNameWithGranularity);
const { rowDomain, colDomain } = domainToColRowDomain(this, domain);
const mainDomain = mainDimension === "row" ? rowDomain : colDomain;
const secondaryDomain = mainDimension === "row" ? colDomain : rowDomain;
const targetValue = mainDomain.find((node) => node.field === fieldNameWithGranularity)?.value;
if (targetValue === undefined) {
return undefined;
}
const secondaryDomainKey = domainToString(secondaryDomain);
const runningTotalKey = getRunningTotalDomainKey(mainDomain, fieldNameWithGranularity);

let previousMainDomainKey: string | undefined;
let previousValue: CellValue | undefined;
const table = this.getCollapsedTableStructure();
const tree = mainDimension === "row" ? table.getRowTree() : table.getColTree();

const visitTree = (nodes: DimensionTree, parentDomain: PivotDomain = []) => {
for (const node of nodes) {
const nodeDomain: PivotDomain = [
...parentDomain,
{ field: node.field, value: node.value, type: node.type },
];
if (node.children.length) {
visitTree(node.children, nodeDomain);
}
const nodeValue = nodeDomain.find(
(domainNode) => domainNode.field === fieldNameWithGranularity
)?.value;
const nodeRunningTotalKey = getRunningTotalDomainKey(
nodeDomain,
fieldNameWithGranularity
);
if (
nodeValue === undefined ||
nodeRunningTotalKey !== runningTotalKey ||
this.compareRunningTotalValues(nodeValue, targetValue, dimension) >= 0
) {
continue;
}
if (
previousValue === undefined ||
this.compareRunningTotalValues(previousValue, nodeValue, dimension) < 0
) {
previousValue = nodeValue;
previousMainDomainKey = domainToString(nodeDomain);
}
}
};

visitTree(tree);

if (!previousMainDomainKey) {
return undefined;
}

return mainDimension === "row"
? runningTotals[secondaryDomainKey]?.[previousMainDomainKey]
: runningTotals[previousMainDomainKey]?.[secondaryDomainKey];
}

private compareRunningTotalValues(
a: CellValue,
b: CellValue,
dimension: PivotDimension
): number {
const order = dimension.order ?? "asc";
const aIsNull = a === null;
const bIsNull = b === null;
if (aIsNull && bIsNull) {
return 0;
}
if (aIsNull) {
return order === "asc" ? 1 : -1;
}
if (bIsNull) {
return order === "asc" ? -1 : 1;
}

const diff =
this.getRunningTotalComparableValue(a, dimension) -
this.getRunningTotalComparableValue(b, dimension);
return order === "asc" ? diff : -diff;
}

private getRunningTotalComparableValue(value: CellValue, dimension: PivotDimension): number {
const granularity = dimension.granularity;
if (granularity && pivotTimeAdapterRegistry.contains(granularity)) {
const adapter = pivotTimeAdapter(granularity as Granularity);
const comparableValue = adapter.toComparableValue?.(value);
if (comparableValue !== undefined) {
return comparableValue;
}
}
return toNumber(value, DEFAULT_LOCALE);
}

private getGrandTotal(measureId: string): number {
const grandTotal = this._getPivotCellValueAndFormat(measureId, []);
return this.measureValueToNumber(grandTotal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import { DEFAULT_LOCALE } from "../../types/locale";
import { Granularity, PivotTimeAdapter, PivotTimeAdapterNotNull } from "../../types/pivot";
import { DAYS, formatValue, MONTHS } from "../format/format";

/**
* Converts "period/year" (e.g. "48/2026", "03/2025", "1/2020")
* into a numeric value that preserves chronological ordering.
*/
export function periodYearToComparable(value: string): number {
const [periodString, yearString] = value.split("/");
return Number(yearString) * 100 + Number(periodString);
}

export const pivotTimeAdapterRegistry = new Registry<PivotTimeAdapter<CellValue>>();

export function pivotTimeAdapter(granularity: Granularity): PivotTimeAdapter<CellValue> {
Expand Down Expand Up @@ -186,6 +195,9 @@ const monthAdapter: PivotTimeAdapterNotNull<string> = {
const jsDate = toJsDate(normalizedValue, DEFAULT_LOCALE);
return `DATE(${jsDate.getFullYear()},${jsDate.getMonth() + 1},1)`;
},
toComparableValue(normalizedValue) {
return periodYearToComparable(normalizedValue);
},
};

/**
Expand Down Expand Up @@ -328,6 +340,12 @@ function nullHandlerDecorator<T>(adapter: PivotTimeAdapterNotNull<T>): PivotTime
}
return adapter.toFunctionValue(normalizedValue);
},
toComparableValue(normalizedValue) {
if (normalizedValue === null) {
return undefined;
}
return adapter.toComparableValue?.(normalizedValue);
},
};
}

Expand Down
2 changes: 2 additions & 0 deletions packages/o-spreadsheet-engine/src/types/pivot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,14 @@ export interface PivotTimeAdapterNotNull<T> {
normalizeFunctionValue: (value: Exclude<CellValue, null>) => T;
toValueAndFormat: (normalizedValue: T, locale?: Locale) => FunctionResultObject;
toFunctionValue: (normalizedValue: T) => string;
toComparableValue?: (normalizedValue: T) => number;
}

export interface PivotTimeAdapter<T> {
normalizeFunctionValue: (value: CellValue) => T | null;
toValueAndFormat: (normalizedValue: T, locale?: Locale) => FunctionResultObject;
toFunctionValue: (normalizedValue: T) => string;
toComparableValue?: (normalizedValue: T | null) => number | undefined;
}

export interface PivotNode {
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ import {
} from "@odoo/o-spreadsheet-engine/helpers/pivot/pivot_helpers";
import { pivotRegistry } from "@odoo/o-spreadsheet-engine/helpers/pivot/pivot_registry";
import {
periodYearToComparable,
pivotTimeAdapter,
pivotTimeAdapterRegistry,
} from "@odoo/o-spreadsheet-engine/helpers/pivot/pivot_time_adapter";
Expand Down Expand Up @@ -399,6 +400,7 @@ export const helpers = {
parseDimension,
isDateOrDatetimeField,
makeFieldProposal,
periodYearToComparable,
insertTokenAfterArgSeparator,
insertTokenAfterLeftParenthesis,
mergeContiguousZones,
Expand Down
129 changes: 129 additions & 0 deletions tests/pivots/pivot_measure/pivot_measure_display_model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,135 @@ describe("Measure display", () => {
A25: "Total", B25: "230200", C25: "230200", D25: "90000", E25: "320200", F25: "320200", G25: "",
});
});

test("PIVOT.VALUE running total falls back to the previous value for a missing date bucket", () => {
const model = createModelWithTestPivotDataset();
setCellContent(model, "A40", `=PIVOT.VALUE(1, "${measureId}", "Created on:month_number", 5)`);
expect(getEvaluatedCell(model, "A40").value).toBe("");
updatePivotMeasureDisplay(model, pivotId, measureId, {
type: "running_total",
fieldNameWithGranularity: "Created on:month_number",
});
expect(getEvaluatedCell(model, "A40").value).toBe(320200);
});

test("PIVOT.VALUE running total supports child row and column domains", () => {
const model = createModelWithTestPivotDataset({
rows: [
{ fieldName: "Created on", granularity: "month_number", order: "asc" },
{ fieldName: "Stage", order: "asc" },
],
columns: [{ fieldName: "Salesperson", order: "asc" }],
});
setCellContent(
model,
"A40",
`=PIVOT.VALUE(1, "${measureId}", "Created on:month_number", 4, "Stage", "New", "Salesperson", "Alice")`
);
setCellContent(
model,
"A41",
`=PIVOT.VALUE(1, "${measureId}", "Created on:month_number", 5, "Stage", "New", "Salesperson", "Alice")`
);

expect(getEvaluatedCell(model, "A40").value).toBe(49000);
expect(getEvaluatedCell(model, "A41").value).toBe("");

updatePivotMeasureDisplay(model, pivotId, measureId, {
type: "running_total",
fieldNameWithGranularity: "Created on:month_number",
});

expect(getEvaluatedCell(model, "A40").value).toBe(154600);
expect(getEvaluatedCell(model, "A41").value).toBe(154600);
});

test("PIVOT.VALUE running total fallback works with multi-level col domain", () => {
const model = createModelWithTestPivotDataset({
rows: [{ fieldName: "Created on", granularity: "month_number", order: "asc" }],
columns: [
{ fieldName: "Salesperson", order: "asc" },
{ fieldName: "Active", order: "asc" },
],
});

setCellContent(
model,
"A40",
`=PIVOT.VALUE(1, "${measureId}", "Created on:month_number", 4, "Salesperson", "Alice", "Active", false)`
);
setCellContent(
model,
"A41",
`=PIVOT.VALUE(1, "${measureId}", "Created on:month_number", 5, "Salesperson", "Alice", "Active", false)`
);

expect(getEvaluatedCell(model, "A40").value).toBe(65000);
expect(getEvaluatedCell(model, "A41").value).toBe("");

updatePivotMeasureDisplay(model, pivotId, measureId, {
type: "running_total",
fieldNameWithGranularity: "Created on:month_number",
});

expect(getEvaluatedCell(model, "A40").value).toBe(193100);
expect(getEvaluatedCell(model, "A41").value).toBe(193100);
});

test("PIVOT.VALUE running total handles Month & Year granularity with column domain", () => {
const model = createModelWithTestPivotDataset({
rows: [{ fieldName: "Created on", granularity: "month", order: "asc" }],
columns: [{ fieldName: "Stage", order: "asc" }],
});
setCellContent(
model,
"A40",
`=PIVOT.VALUE(1, "${measureId}", "Created on:month", DATE(2024,4,1), "Stage", "New")`
);
setCellContent(
model,
"A41",
`=PIVOT.VALUE(1, "${measureId}", "Created on:month", DATE(2024,5,1), "Stage", "New")`
);

expect(getEvaluatedCell(model, "A40").value).toBe(73000);
expect(getEvaluatedCell(model, "A41").value).toBe("");

updatePivotMeasureDisplay(model, pivotId, measureId, {
type: "running_total",
fieldNameWithGranularity: "Created on:month",
});

expect(getEvaluatedCell(model, "A40").value).toBe(204600);
expect(getEvaluatedCell(model, "A41").value).toBe(204600);
});

test("PIVOT.VALUE running total returns empty before the first date bucket", () => {
const model = createModelWithTestPivotDataset();
setCellContent(model, "A40", `=PIVOT.VALUE(1, "${measureId}", "Created on:month_number", 1)`);
expect(getEvaluatedCell(model, "A40").value).toBe("");
updatePivotMeasureDisplay(model, pivotId, measureId, {
type: "running_total",
fieldNameWithGranularity: "Created on:month_number",
});
expect(getEvaluatedCell(model, "A40").value).toBe("");
});

test("PIVOT.VALUE running total picks the previous bucket in descending date order", () => {
const model = createModelWithTestPivotDataset({
rows: [{ fieldName: "Created on", granularity: "month_number", order: "desc" }],
});
setCellContent(model, "A40", `=PIVOT.VALUE(1, "${measureId}", "Created on:month_number", 1)`);
setCellContent(model, "A41", `=PIVOT.VALUE(1, "${measureId}", "Created on:month_number", 5)`);
expect(getEvaluatedCell(model, "A40").value).toBe("");
expect(getEvaluatedCell(model, "A41").value).toBe("");
updatePivotMeasureDisplay(model, pivotId, measureId, {
type: "running_total",
fieldNameWithGranularity: "Created on:month_number",
});
expect(getEvaluatedCell(model, "A40").value).toBe(320200);
expect(getEvaluatedCell(model, "A41").value).toBe("");
});
});

describe("%_running_total", () => {
Expand Down