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.

fix: fix SUBTOTAL formula need ignore other SUBTOTAL formula results (#5506)

wpxp123456732bcec66441
diff --git a/packages/engine-formula/src/functions/math/subtotal/index.ts b/packages/engine-formula/src/functions/math/subtotal/index.ts
index fbfa3a622a..3e5d961cd7 100644
--- a/packages/engine-formula/src/functions/math/subtotal/index.ts
+++ b/packages/engine-formula/src/functions/math/subtotal/index.ts
@@ -18,6 +18,7 @@ import type { IObjectArrayPrimitiveType, IRowData, Nullable } from '@univerjs/co
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 type { FormulaDataModel } from '../../../models/formula-data.model';
import { BooleanNumber } from '@univerjs/core';
import { ErrorType } from '../../../basics/error-type';
import { createNewArray } from '../../../engine/utils/array-object';
@@ -62,6 +63,8 @@ export class Subtotal extends BaseFunction {
override needsFilteredOutRows = true;
+ override needsFormulaDataModel = true;
+
override calculate(functionNum: FunctionVariantType, ...refs: FunctionVariantType[]) {
if (functionNum.isError()) {
return functionNum;
@@ -381,10 +384,14 @@ export class Subtotal extends BaseFunction {
const filteredOutRows = (variant as BaseReferenceObject).getFilteredOutRows();
const rowData = (variant as BaseReferenceObject).getRowData();
+ const unitId = (variant as BaseReferenceObject).getUnitId();
+ const sheetId = (variant as BaseReferenceObject).getSheetId();
+ const unitData = (variant as BaseReferenceObject).getUnitData();
+ const cellData = unitData[unitId]?.[sheetId]?.cellData;
let errorValue: Nullable<BaseValueObject>;
- (variant as BaseReferenceObject).iterator((valueObject, rowIndex) => {
+ (variant as BaseReferenceObject).iterator((valueObject, rowIndex, columnIndex) => {
// Filtered rows are always excluded.
if (filteredOutRows.includes(rowIndex)) {
return true; // continue
@@ -394,6 +401,17 @@ export class Subtotal extends BaseFunction {
return true; // continue
}
+ // Ignore other SUBTOTAL formula results
+ const cellValue = cellData.getValue(rowIndex, columnIndex);
+ if (cellValue?.f || cellValue?.si) {
+ const formulaString = (this._formulaDataModel as FormulaDataModel).getFormulaStringByCell(rowIndex, columnIndex, sheetId, unitId);
+
+ // match 'SUBTOTAL(' for simple check
+ if (formulaString && formulaString.indexOf(`${this.name}(`) > -1) {
+ return true; // continue
+ }
+ }
+
// 'test', ' ', blank cell, TRUE and FALSE are ignored
if (valueObject == null || valueObject.isNull() || valueObject.isString() || valueObject.isBoolean()) {
return true;
diff --git a/packages/sheets/src/controllers/__tests__/formula/array-inverted-index-cache.spec.ts b/packages/sheets/src/controllers/__tests__/formula/array-inverted-index-cache.spec.ts
index 6f0afe951c..49289ff844 100644
--- a/packages/sheets/src/controllers/__tests__/formula/array-inverted-index-cache.spec.ts
+++ b/packages/sheets/src/controllers/__tests__/formula/array-inverted-index-cache.spec.ts
@@ -61,18 +61,30 @@ const getFunctionsTestWorkbookData = (): IWorkbookData => {
v: 1,
t: 2,
},
+ 4: { // E1
+ v: 1,
+ t: 2,
+ },
},
1: {
0: {
v: 'B2',
t: 1,
},
+ 4: { // E2
+ v: 2,
+ t: 2,
+ },
},
2: {
0: {
v: 'B3',
t: 1,
},
+ 4: { // E3
+ v: 3,
+ t: 2,
+ },
},
3: {
0: {
@@ -83,12 +95,18 @@ const getFunctionsTestWorkbookData = (): IWorkbookData => {
v: 3,
t: 2,
},
+ 4: { // E4
+ f: '=SUM(SUBTOTAL(9,E1:E3),TRUE())',
+ },
},
4: {
0: {
v: 'B2',
t: 1,
},
+ 4: { // E5
+ f: '=SUBTOTAL(9,E1:E4)',
+ },
},
5: {
0: {
@@ -362,5 +380,12 @@ describe('Test inverted index cache', () => {
result = calculate('=SUMIF($A$1:A13,A13,$B$1:B13)');
expect(result).toBe(25);
});
+
+ it('Subtotal formula test', async () => {
+ // E4 cell formula: =SUM(SUBTOTAL(9,E1:E3),TRUE())
+ expect(getCellValue(3, 4)).toBe(7);
+ // E5 cell formula: =SUBTOTAL(9,E1:E4)
+ expect(getCellValue(4, 4)).toBe(6);
+ });
});
});