Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat(formula): implement GETPIVOTDATA (#8074)

wpxp1234560e7370f0fcd3
diff --git a/packages/engine-formula/src/functions/lookup/function-map.ts b/packages/engine-formula/src/functions/lookup/function-map.ts
index 820741cbd4..1c068cbd5d 100644
--- a/packages/engine-formula/src/functions/lookup/function-map.ts
+++ b/packages/engine-formula/src/functions/lookup/function-map.ts
@@ -27,7 +27,7 @@ import { Expand } from './expand';
import { Filter } from './filter';
import { Formulatext } from './formulatext';
import { FUNCTION_NAMES_LOOKUP } from './function-names';
-// import { Getpivotdata } from './getpivotdata';
+import { Getpivotdata } from './getpivotdata';
import { Hlookup } from './hlookup';
import { Hstack } from './hstack';
import { Hyperlink } from './hyperlink';
@@ -67,7 +67,7 @@ export const functionLookup = [
[Expand, FUNCTION_NAMES_LOOKUP.EXPAND],
[Filter, FUNCTION_NAMES_LOOKUP.FILTER],
[Formulatext, FUNCTION_NAMES_LOOKUP.FORMULATEXT],
- // [Getpivotdata, FUNCTION_NAMES_LOOKUP.GETPIVOTDATA],
+ [Getpivotdata, FUNCTION_NAMES_LOOKUP.GETPIVOTDATA],
[Hlookup, FUNCTION_NAMES_LOOKUP.HLOOKUP],
[Hstack, FUNCTION_NAMES_LOOKUP.HSTACK],
[Hyperlink, FUNCTION_NAMES_LOOKUP.HYPERLINK],
diff --git a/packages/engine-formula/src/functions/lookup/getpivotdata/__tests__/index.spec.ts b/packages/engine-formula/src/functions/lookup/getpivotdata/__tests__/index.spec.ts
new file mode 100644
index 0000000000..c8cc9424ba
--- /dev/null
+++ b/packages/engine-formula/src/functions/lookup/getpivotdata/__tests__/index.spec.ts
@@ -0,0 +1,292 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { ICellData, IWorkbookData, Nullable } from '@univerjs/core';
+import { CellValueType, LocaleType, ObjectMatrix } from '@univerjs/core';
+import { describe, expect, it } from 'vitest';
+import type { IUnitData } from '../../../../basics/common';
+import type { BaseReferenceObject } from '../../../../engine/reference-object/base-reference-object';
+import type { BaseValueObject } from '../../../../engine/value-object/base-value-object';
+import { createFunctionTestBed } from '../../../__tests__/create-function-test-bed';
+import { ErrorType } from '../../../../basics/error-type';
+import { Lexer } from '../../../../engine/analysis/lexer';
+import { LexerNode } from '../../../../engine/analysis/lexer-node';
+import { AstTreeBuilder } from '../../../../engine/analysis/parser';
+import { Interpreter } from '../../../../engine/interpreter/interpreter';
+import { CellReferenceObject } from '../../../../engine/reference-object/cell-reference-object';
+import { generateExecuteAstNodeData } from '../../../../engine/utils/ast-node-tool';
+import { StringValueObject } from '../../../../engine/value-object/primitive-object';
+import { IFormulaCurrentConfigService } from '../../../../services/current-data.service';
+import { IFunctionService } from '../../../../services/function.service';
+import { IFormulaRuntimeService } from '../../../../services/runtime.service';
+import { ALL_IMPLEMENTED_FUNCTIONS } from '../../../index';
+import { getObjectValue } from '../../../util';
+import { FUNCTION_NAMES_LOOKUP } from '../../function-names';
+import { Getpivotdata } from '../index';
+
+const UNIT_ID = 'pivot-unit';
+const SHEET_ID = 'pivot-sheet';
+
+function text(value: string, indent?: number): ICellData {
+ if (indent == null) {
+ return { v: value, t: CellValueType.STRING };
+ }
+ return { v: value, t: CellValueType.STRING, s: { pd: { l: indent } } };
+}
+
+function number(value: number): ICellData {
+ return { v: value, t: CellValueType.NUMBER };
+}
+
+function anchor(cells: Array<[number, number, Nullable<ICellData>]>, reference = 'A1'): BaseReferenceObject {
+ const cellData: Record<number, Record<number, ICellData>> = {};
+ for (const [row, column, cell] of cells) {
+ if (cell == null) {
+ continue;
+ }
+ cellData[row] = cellData[row] ?? {};
+ cellData[row][column] = cell;
+ }
+
+ const unitData: IUnitData = {
+ [UNIT_ID]: {
+ [SHEET_ID]: {
+ cellData: new ObjectMatrix(cellData),
+ rowCount: 100,
+ columnCount: 30,
+ rowData: {},
+ columnData: {},
+ },
+ },
+ };
+
+ const referenceObject = new CellReferenceObject(reference);
+ referenceObject.setDefaultUnitId(UNIT_ID);
+ referenceObject.setDefaultSheetId(SHEET_ID);
+ referenceObject.setUnitData(unitData);
+
+ return referenceObject;
+}
+
+const textArg = (value: string) => StringValueObject.create(value);
+
+describe('GETPIVOTDATA', () => {
+ const testFunction = new Getpivotdata(FUNCTION_NAMES_LOOKUP.GETPIVOTDATA);
+
+ const compactReport = anchor([
+ [0, 0, text('Row Labels')],
+ [0, 1, text('Count of Status')],
+ [1, 0, text('E2E')],
+ [1, 1, number(25)],
+ [2, 0, text('Current', 1)],
+ [2, 1, number(23)],
+ [3, 0, text('IMC')],
+ [3, 1, number(4)],
+ [4, 0, text('Current', 1)],
+ [4, 1, number(2)],
+ [5, 0, text('Closed', 1)],
+ [5, 1, number(2)],
+ [6, 0, text('Grand Total')],
+ [6, 1, number(56)],
+ ]);
+
+ it('resolves the indented item instead of the parent subtotal', () => {
+ const result = testFunction.calculate(
+ textArg('Status'),
+ compactReport,
+ textArg('Department'),
+ textArg('IMC'),
+ textArg('Status'),
+ textArg('Current')
+ ) as BaseValueObject;
+
+ expect(getObjectValue(result)).toBe(2);
+ });
+
+ it('accepts the source field name of an aggregated caption', () => {
+ const result = testFunction.calculate(
+ textArg('Status'),
+ compactReport
+ ) as BaseValueObject;
+
+ expect(getObjectValue(result)).toBe(56);
+ });
+
+ it('reads a rowless report from the measure caption row', () => {
+ const rowlessReport = anchor([
+ [1, 10, text('Sum of Created Value')],
+ [1, 11, text('Sum of Built Cost')],
+ [2, 10, number(700006621.4084)],
+ [2, 11, number(427057283.8337)],
+ ], 'K2');
+
+ const result = testFunction.calculate(
+ textArg('Sum of Built Cost'),
+ rowlessReport
+ ) as BaseValueObject;
+
+ expect(getObjectValue(result)).toBeCloseTo(427057283.8337, 4);
+ });
+
+ it('reads a tabular report by field header and item label', () => {
+ const report = anchor([
+ [0, 0, text('Department')],
+ [0, 1, text('Status')],
+ [0, 2, text('Count')],
+ [1, 0, text('IMC')],
+ [1, 1, text('Current')],
+ [1, 2, number(2)],
+ ]);
+
+ const result = testFunction.calculate(
+ textArg('Count'),
+ report,
+ textArg('Department'),
+ textArg('IMC'),
+ textArg('Status'),
+ textArg('Current')
+ ) as BaseValueObject;
+
+ expect(getObjectValue(result)).toBe(2);
+ });
+
+ it('selects a data column when the item is a column field', () => {
+ const report = anchor([
+ [0, 0, text('Department')],
+ [0, 1, text('Sum of Cost')],
+ [0, 2, text('Current')],
+ [0, 3, text('Closed')],
+ [1, 0, text('IMC')],
+ [1, 1, number(10)],
+ [1, 2, number(6)],
+ [1, 3, number(4)],
+ ]);
+
+ const result = testFunction.calculate(
+ textArg('Cost'),
+ report,
+ textArg('Department'),
+ textArg('IMC'),
+ textArg('Status'),
+ textArg('Closed')
+ ) as BaseValueObject;
+
+ expect(getObjectValue(result)).toBe(4);
+ });
+
+ it('rejects subtotal items and unknown fields', () => {
+ const subtotal = testFunction.calculate(
+ textArg('Status'),
+ compactReport,
+ textArg('Department'),
+ textArg('General Total')
+ ) as BaseValueObject;
+ const unknown = testFunction.calculate(
+ textArg('Missing'),
+ compactReport
+ ) as BaseValueObject;
+
+ expect(getObjectValue(subtotal)).toBe(ErrorType.REF);
+ expect(getObjectValue(unknown)).toBe(ErrorType.REF);
+ });
+
+ it('requires a single-cell reference as the pivot table anchor', () => {
+ const notReference = testFunction.calculate(
+ textArg('Status'),
+ textArg('A1')
+ ) as BaseValueObject;
+ const notSingleCell = testFunction.calculate(
+ textArg('Status'),
+ anchor([], 'A1:B2')
+ ) as BaseValueObject;
+ const oddArguments = testFunction.calculate(
+ textArg('Status'),
+ compactReport,
+ textArg('Department')
+ ) as BaseValueObject;
+
+ expect(getObjectValue(notReference)).toBe(ErrorType.VALUE);
+ expect(getObjectValue(notSingleCell)).toBe(ErrorType.VALUE);
+ expect(getObjectValue(oddArguments)).toBe(ErrorType.VALUE);
+ });
+
+ it('is registered and evaluates through the formula interpreter', () => {
+ const workbookData: IWorkbookData = {
+ id: 'test',
+ appVersion: '3.0.0-alpha',
+ locale: LocaleType.EN_US,
+ name: 'pivot-formula',
+ sheetOrder: ['sheet1'],
+ styles: {},
+ sheets: {
+ sheet1: {
+ id: 'sheet1',
+ name: 'Pivot',
+ rowCount: 20,
+ columnCount: 10,
+ cellData: {
+ 0: {
+ 0: text('Row Labels'),
+ 1: text('Count of Status'),
+ 6: { v: '=GETPIVOTDATA("Status",A1,"Department","IMC","Status","Current")' },
+ },
+ 1: {
+ 0: text('IMC'),
+ 1: number(4),
+ },
+ 2: {
+ 0: text('Current', 1),
+ 1: number(2),
+ },
+ },
+ },
+ },
+ };
+
+ const testBed = createFunctionTestBed(workbookData);
+ const currentConfigService = testBed.get(IFormulaCurrentConfigService);
+ const runtimeService = testBed.get(IFormulaRuntimeService);
+ const functionService = testBed.get(IFunctionService);
+
+ functionService.registerExecutors(...ALL_IMPLEMENTED_FUNCTIONS.map(([FunctionCtor, name]) => new FunctionCtor(name)));
+ currentConfigService.load({
+ formulaData: {},
+ arrayFormulaCellData: {},
+ arrayFormulaRange: {},
+ forceCalculate: false,
+ dirtyRanges: [],
+ dirtyNameMap: {},
+ dirtyDefinedNameMap: {},
+ dirtyUnitFeatureMap: {},
+ dirtyUnitOtherFormulaMap: {},
+ excludedCell: {},
+ allUnitData: { [testBed.unitId]: testBed.sheetData },
+ });
+ runtimeService.setCurrent(0, 6, 1, 1, testBed.sheetId, testBed.unitId);
+
+ const lexerNode = testBed.get(Lexer).treeBuilder('=GETPIVOTDATA("Status",A1,"Department","IMC","Status","Current")');
+ expect(lexerNode).toBeInstanceOf(LexerNode);
+
+ const astNode = testBed.get(AstTreeBuilder).parse(lexerNode as LexerNode);
+ if (astNode == null) {
+ throw new Error('Failed to build GETPIVOTDATA AST');
+ }
+ const result = testBed.get(Interpreter).execute(generateExecuteAstNodeData(astNode));
+
+ expect(getObjectValue(result as BaseValueObject)).toBe(2);
+ testBed.univer.dispose();
+ });
+});
diff --git a/packages/engine-formula/src/functions/lookup/getpivotdata/index.ts b/packages/engine-formula/src/functions/lookup/getpivotdata/index.ts
index b4522c8e2d..beff3e670d 100644
--- a/packages/engine-formula/src/functions/lookup/getpivotdata/index.ts
+++ b/packages/engine-formula/src/functions/lookup/getpivotdata/index.ts
@@ -14,12 +14,504 @@
* limitations under the License.
*/
-import { NotImplementedFunction } from '../../not-implemented-function';
+import type { ICellData, IRange, Nullable, Styles } from '@univerjs/core';
+import type { BaseReferenceObject, FunctionVariantType } from '../../../engine/reference-object/base-reference-object';
+import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
+import { ErrorType } from '../../../basics/error-type';
+import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
+import { NullValueObject } from '../../../engine/value-object/primitive-object';
+import { BaseFunction } from '../../base-function';
-export class Getpivotdata extends NotImplementedFunction {
+const PIVOT_SCAN_ROW_LIMIT = 200;
+const PIVOT_SCAN_COLUMN_LIMIT = 50;
+const PIVOT_TOTAL_SUFFIX = ' TOTAL';
+const PIVOT_GRAND_TOTAL_LABEL = 'Grand Total';
+const PIVOT_YEAR_FIELD = 'YEAR';
+
+/**
+ * Excel reports aggregated value fields as `Sum of <field>`, `Count of <field>`, etc.
+ * `GETPIVOTDATA` also accepts the source field name in that position.
+ */
+const PIVOT_AGGREGATE_PREFIXES = [
+ 'sum of ',
+ 'count of ',
+ 'average of ',
+ 'max of ',
+ 'min of ',
+ 'product of ',
+ 'count numbers of ',
+ 'stddev of ',
+ 'stddevp of ',
+ 'var of ',
+];
+
+type PivotFieldPair = [string, string];
+
+interface IPivotColumnItemMatch {
+ index: number;
+ column: number;
+}
+
+interface IPivotCellReader {
+ text(row: number, column: number): string;
+ leftPadding(row: number, column: number): number;
+ value(row: number, column: number): BaseValueObject;
+}
+
+/**
+ * The visible PivotTable report is scanned from the anchor cell. The reader resolves the
+ * runtime value first so cells recalculated in this session win over the stored snapshot.
+ */
+class PivotCellReader implements IPivotCellReader {
+ private readonly _styles: Nullable<Styles>;
+
+ constructor(private readonly _anchor: BaseReferenceObject) {
+ this._styles = _anchor.getCurrentStylesData();
+ }
+
+ text(row: number, column: number): string {
+ const cell = this._readCell(row, column);
+ if (cell?.v == null) {
+ return '';
+ }
+ return String(cell.v);
+ }
+
+ leftPadding(row: number, column: number): number {
+ const cell = this._storedCell(row, column);
+ if (cell == null) {
+ return 0;
+ }
+ const style = typeof cell.s === 'object' && cell.s != null
+ ? cell.s
+ : this._styles?.getStyleByCell(cell);
+ const padding = style?.pd?.l;
+ return typeof padding === 'number' ? padding : 0;
+ }
+
+ value(row: number, column: number): BaseValueObject {
+ const cell = this._readCell(row, column);
+ if (cell == null) {
+ return NullValueObject.create();
+ }
+ return this._anchor.getCellValueObject(cell);
+ }
+
+ private _readCell(row: number, column: number): Nullable<ICellData> {
+ const runtimeCell = this._anchor.getCurrentRuntimeSheetData()?.getValue(row, column);
+ return runtimeCell ?? this._storedCell(row, column);
+ }
+
+ private _storedCell(row: number, column: number): Nullable<ICellData> {
+ return this._anchor.getCurrentActiveSheetData()?.cellData.getValue(row, column) ?? null;
+ }
+}
+
+export class Getpivotdata extends BaseFunction {
override minParams = 2;
override maxParams = 254;
- // TODO: Implement pivot-table context lookup and field/item filtering.
+ override needsReferenceObject = true;
+
+ override calculate(...variants: FunctionVariantType[]): BaseValueObject {
+ if (variants.length < 2 || variants.length % 2 !== 0) {
+ return ErrorValueObject.create(ErrorType.VALUE);
+ }
+
+ const anchor = variants[1];
+ if (!anchor.isReferenceObject()) {
+ return ErrorValueObject.create(ErrorType.VALUE);
+ }
+
+ const reference = anchor as BaseReferenceObject;
+ const range = reference.getRangePosition();
+ if (range.startRow !== range.endRow || range.startColumn !== range.endColumn) {
+ return ErrorValueObject.create(ErrorType.VALUE);
+ }
+
+ const fieldPairs: PivotFieldPair[] = [];
+ for (let index = 2; index < variants.length; index += 2) {
+ fieldPairs.push([this._textArgument(variants[index]), this._textArgument(variants[index + 1])]);
+ }
+
+ const result = visiblePivotGet(
+ new PivotCellReader(reference),
+ range,
+ this._textArgument(variants[0]).trim(),
+ fieldPairs
+ );
+
+ return result ?? ErrorValueObject.create(ErrorType.REF);
+ }
+
+ private _textArgument(variant: FunctionVariantType): string {
+ let valueObject: BaseValueObject;
+ if (variant.isReferenceObject()) {
+ valueObject = (variant as BaseReferenceObject).getCellByPosition();
+ } else {
+ valueObject = variant as BaseValueObject;
+ }
+
+ if (valueObject.isArray()) {
+ valueObject = (valueObject as ArrayValueObject).getFirstCell();
+ }
+
+ const value = valueObject.getValue();
+ return value == null ? '' : String(value);
+ }
+}
+
+function visiblePivotGet(
+ reader: IPivotCellReader,
+ anchorRange: IRange,
+ dataField: string,
+ fieldPairs: PivotFieldPair[]
+): Nullable<BaseValueObject> {
+ if (fieldPairs.some((pair) => isInvalidPivotItem(pair[1]))) {
+ return null;
+ }
+
+ const startRow = anchorRange.startRow;
+ const startColumn = anchorRange.startColumn;
+ const maxRow = startRow + PIVOT_SCAN_ROW_LIMIT;
+ const maxColumn = startColumn + PIVOT_SCAN_COLUMN_LIMIT;
+
+ for (let headerRow = startRow; headerRow <= maxRow; headerRow++) {
+ const dataColumn = findPivotDataColumn(reader, headerRow, startColumn, maxColumn, dataField);
+ if (dataColumn == null) {
+ continue;
+ }
+
+ const columnMatch = findPivotColumnItemMatch(reader, headerRow, dataColumn, maxRow, maxColumn, fieldPairs);
+ const resolvedColumn = columnMatch?.column ?? dataColumn;
+ const rowPairs = columnMatch == null
+ ? fieldPairs
+ : fieldPairs.filter((_, index) => index !== columnMatch.index);
+
+ let row: Nullable<number>;
+ if (rowPairs.length === 0) {
+ row = findRowlessPivotRow(reader, headerRow, startColumn, maxRow)
+ ?? findGrandTotalRow(reader, headerRow, startColumn, maxRow);
+ } else {
+ row = findTabularPivotRow(reader, headerRow, startColumn, resolvedColumn, maxRow, rowPairs)
+ ?? findHierarchyPivotRow(reader, headerRow, startColumn, maxRow, rowPairs);
+ }
+
+ if (row == null) {
+ return null;
+ }
+
+ return reader.value(row, resolvedColumn);
+ }
+
+ return null;
+}
+
+function findPivotDataColumn(
+ reader: IPivotCellReader,
+ headerRow: number,
+ startColumn: number,
+ maxColumn: number,
+ dataField: string
+): Nullable<number> {
+ for (let column = startColumn; column <= maxColumn; column++) {
+ if (samePivotDataField(reader.text(headerRow, column), dataField)) {
+ return column;
+ }
+ }
+ return null;
+}
+
+/**
+ * A column field renders its items as a header row above the first data row. When the
+ * requested item is found there, it selects a data column instead of a row.
+ */
+function findPivotColumnItemMatch(
+ reader: IPivotCellReader,
+ headerRow: number,
+ dataColumn: number,
+ maxRow: number,
+ maxColumn: number,
+ fieldPairs: PivotFieldPair[]
+): Nullable<IPivotColumnItemMatch> {
+ const firstDataRow = findFirstPivotDataRow(reader, headerRow, dataColumn, maxRow, maxColumn);
+ if (firstDataRow == null) {
+ return null;
+ }
+
+ const lastHeaderRow = firstDataRow - 1;
+ if (dataColumn >= maxColumn || lastHeaderRow < headerRow) {
+ return null;
+ }
+
+ for (let index = 0; index < fieldPairs.length; index++) {
+ const item = fieldPairs[index][1];
+ for (let row = headerRow; row <= lastHeaderRow; row++) {
+ for (let column = dataColumn + 1; column <= maxColumn; column++) {
+ if (samePivotText(reader.text(row, column), item)) {
+ return { index, column };
+ }
+ }
+ }
+ }
+
+ return null;
+}
+
+function findFirstPivotDataRow(
+ reader: IPivotCellReader,
+ headerRow: number,
+ dataColumn: number,
+ maxRow: number,
+ maxColumn: number
+): Nullable<number> {
+ for (let row = headerRow + 1; row <= maxRow; row++) {
+ for (let column = dataColumn; column <= maxColumn; column++) {
+ if (parsePivotNumber(reader.text(row, column)) != null) {
+ return row;
+ }
+ }
+ }
+ return null;
+}
+
+/**
+ * A PivotTable without row fields renders the measure captions above a single value row
+ * and does not emit a `Grand Total` label.
+ */
+function findRowlessPivotRow(
+ reader: IPivotCellReader,
+ headerRow: number,
+ startColumn: number,
+ maxRow: number
+): Nullable<number> {
+ if (headerRow + 2 > maxRow) {
+ return null;
+ }
+ if (pivotAggregateSourceName(reader.text(headerRow, startColumn)) == null) {
+ return null;
+ }
+ if (parsePivotNumber(reader.text(headerRow + 1, startColumn)) == null) {
+ return null;
+ }
+ if (reader.text(headerRow + 2, startColumn).trim().length > 0) {
+ return null;
+ }
+ return headerRow + 1;
+}
+
+function findGrandTotalRow(
+ reader: IPivotCellReader,
+ headerRow: number,
+ startColumn: number,
+ maxRow: number
+): Nullable<number> {
+ for (let row = headerRow + 1; row <= maxRow; row++) {
+ if (samePivotText(reader.text(row, startColumn), PIVOT_GRAND_TOTAL_LABEL)) {
+ return row;
+ }
+ }
+ return null;
+}
+
+/**
+ * Tabular reports repeat the field name as a header and the item as a label, followed by
+ * an `<item> Total` subtotal row when subtotals are enabled.
+ */
+function findTabularPivotRow(
+ reader: IPivotCellReader,
+ headerRow: number,
+ startColumn: number,
+ dataColumn: number,
+ maxRow: number,
+ fieldPairs: PivotFieldPair[]
+): Nullable<number> {
+ const [field, item] = fieldPairs[0];
+ const itemText = item.trim();
+
+ let fieldColumn: Nullable<number> = null;
+ for (let column = startColumn; column <= dataColumn; column++) {
+ if (samePivotText(reader.text(headerRow, column), field)) {
+ fieldColumn = column;
+ break;
+ }
+ }
+ if (fieldColumn == null) {
+ return null;
+ }
+
+ let matchedRow: Nullable<number> = null;
+ for (let row = headerRow + 1; row <= maxRow; row++) {
+ if (samePivotText(reader.text(row, fieldColumn), itemText)) {
+ matchedRow = row;
+ break;
+ }
+ }
+ if (matchedRow == null) {
+ return null;
+ }
+
+ const subtotalLabel = `${itemText} Total`;
+ for (let row = matchedRow + 1; row <= maxRow; row++) {
+ const label = reader.text(row, fieldColumn).trim();
+ if (label.length === 0) {
+ continue;
+ }
+ if (samePivotText(label, subtotalLabel)) {
+ return row;
+ }
+ break;
+ }
+
+ return matchedRow;
+}
+
+function findHierarchyPivotRow(
+ reader: IPivotCellReader,
+ headerRow: number,
+ startColumn: number,
+ maxRow: number,
+ fieldPairs: PivotFieldPair[]
+): Nullable<number> {
+ if (hasPivotIndentation(reader, headerRow, startColumn, maxRow)) {
+ return findIndentedPivotRow(reader, headerRow, startColumn, maxRow, fieldPairs);
+ }
+ return findSequentialPivotRow(reader, headerRow, startColumn, maxRow, fieldPairs);
+}
+
+/**
+ * Compact reports encode row ancestry as left indentation, so only indented labels are
+ * meaningful ancestors of the current row.
+ */
+function hasPivotIndentation(
+ reader: IPivotCellReader,
+ headerRow: number,
+ startColumn: number,
+ maxRow: number
+): boolean {
+ for (let row = headerRow + 1; row <= maxRow; row++) {
+ if (reader.text(row, startColumn).trim().length === 0) {
+ return false;
+ }
+ if (reader.leftPadding(row, startColumn) > 0) {
+ return true;
+ }
+ }
+ return false;
+}
+
+function findIndentedPivotRow(
+ reader: IPivotCellReader,
+ headerRow: number,
+ startColumn: number,
+ maxRow: number,
+ fieldPairs: PivotFieldPair[]
+): Nullable<number> {
+ const ancestors: Array<{ padding: number; label: string }> = [];
+ for (let row = headerRow + 1; row <= maxRow; row++) {
+ const label = reader.text(row, startColumn);
+ if (label.trim().length === 0) {
+ continue;
+ }
+
+ const padding = reader.leftPadding(row, startColumn);
+ while (ancestors.length > 0 && ancestors[ancestors.length - 1].padding >= padding) {
+ ancestors.pop();
+ }
+ ancestors.push({ padding, label });
+
+ const matched = fieldPairs.every((pair) =>
+ ancestors.some((ancestor) => samePivotText(ancestor.label, pair[1])));
+ if (matched) {
+ return row;
+ }
+ }
+ return null;
+}
+
+/**
+ * Non-indented reports fall back to the original layout inference: remember the last
+ * matching item per field and require every requested item to be active on the same row.
+ */
+function findSequentialPivotRow(
+ reader: IPivotCellReader,
+ headerRow: number,
+ startColumn: number,
+ maxRow: number,
+ fieldPairs: PivotFieldPair[]
+): Nullable<number> {
+ const active: Array<Nullable<string>> = fieldPairs.map(() => null);
+
+ for (let row = headerRow + 1; row <= maxRow; row++) {
+ const label = reader.text(row, startColumn).trim();
+ if (label.length === 0) {
+ continue;
+ }
+
+ fieldPairs.forEach((pair, index) => {
+ if (!samePivotText(label, pair[1].trim())) {
+ return;
+ }
+ active[index] = pair[1].trim();
+ if (!samePivotText(pair[0], PIVOT_YEAR_FIELD)) {
+ clearYearItems(active, fieldPairs);
+ }
+ });
+
+ const matched = fieldPairs.every((pair, index) => {
+ const activeItem = active[index];
+ return activeItem != null && samePivotText(activeItem, pair[1].trim());
+ });
+ if (matched) {
+ return row;
+ }
+ }
+ return null;
+}
+
+function clearYearItems(active: Array<Nullable<string>>, fieldPairs: PivotFieldPair[]): void {
+ fieldPairs.forEach((pair, index) => {
+ if (samePivotText(pair[0], PIVOT_YEAR_FIELD)) {
+ active[index] = null;
+ }
+ });
+}
+
+function isInvalidPivotItem(item: string): boolean {
+ const text = item.trim();
+ return text.length === 0 || text.toUpperCase().endsWith(PIVOT_TOTAL_SUFFIX);
+}
+
+function parsePivotNumber(text: string): Nullable<number> {
+ const trimmed = text.trim();
+ if (trimmed.length === 0) {
+ return null;
+ }
+ const value = Number(trimmed);
+ return Number.isFinite(value) ? value : null;
+}
+
+function samePivotText(left: string, right: string): boolean {
+ return left.trim().toLowerCase() === right.trim().toLowerCase();
+}
+
+function samePivotDataField(visibleCaption: string, requestedField: string): boolean {
+ if (samePivotText(visibleCaption, requestedField)) {
+ return true;
+ }
+ const sourceName = pivotAggregateSourceName(visibleCaption);
+ return sourceName != null && samePivotText(sourceName, requestedField);
+}
+
+function pivotAggregateSourceName(caption: string): Nullable<string> {
+ const text = caption.trim();
+ const lowerCaseText = text.toLowerCase();
+ for (const prefix of PIVOT_AGGREGATE_PREFIXES) {
+ if (lowerCaseText.startsWith(prefix)) {
+ return text.slice(prefix.length).trim();
+ }
+ }
+ return null;
}