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(sheet): array formula exceed (#1032)

Univereb92c7b1102b
diff --git a/packages/engine-formula/src/engine/analysis/__tests__/lexer.spec.ts b/packages/engine-formula/src/engine/analysis/__tests__/lexer.spec.ts
index 86127a45c5..4a98746f13 100644
--- a/packages/engine-formula/src/engine/analysis/__tests__/lexer.spec.ts
+++ b/packages/engine-formula/src/engine/analysis/__tests__/lexer.spec.ts
@@ -83,7 +83,7 @@ describe('lexer nodeMaker test', () => {
lexerTreeBuilder = get(LexerTreeBuilder);
- runtimeService.setCurrent(0, 0, 'sheet1', 'test');
+ runtimeService.setCurrent(0, 0, 4, 1, 'sheet1', 'test');
lexer = new Lexer(definedNamesService, runtimeService, lexerTreeBuilder);
});
diff --git a/packages/engine-formula/src/engine/dependency/dependency-tree.ts b/packages/engine-formula/src/engine/dependency/dependency-tree.ts
index 2fc57f5ff7..2e76adcd59 100644
--- a/packages/engine-formula/src/engine/dependency/dependency-tree.ts
+++ b/packages/engine-formula/src/engine/dependency/dependency-tree.ts
@@ -48,6 +48,10 @@ export class FormulaDependencyTree extends Disposable {
column: number = -1;
+ rowCount: number = -Infinity;
+
+ columnCount: number = -Infinity;
+
subUnitId: string = '';
unitId: string = '';
diff --git a/packages/engine-formula/src/engine/dependency/formula-dependency.ts b/packages/engine-formula/src/engine/dependency/formula-dependency.ts
index aae185846d..a3e4d1d9b1 100644
--- a/packages/engine-formula/src/engine/dependency/formula-dependency.ts
+++ b/packages/engine-formula/src/engine/dependency/formula-dependency.ts
@@ -19,7 +19,7 @@ import { Disposable, LifecycleStages, ObjectMatrix, OnLifecycle } from '@univerj
import { Inject } from '@wendellhu/redi';
import { FormulaAstLRU } from '../../basics/cache-lru';
-import type { IDirtyUnitSheetNameMap, IFormulaData, IOtherFormulaData } from '../../basics/common';
+import type { IDirtyUnitSheetNameMap, IFormulaData, IOtherFormulaData, IUnitData } from '../../basics/common';
import { ErrorType } from '../../basics/error-type';
import { prefixToken, suffixToken } from '../../basics/token';
import { IFormulaCurrentConfigService } from '../../services/current-data.service';
@@ -76,7 +76,9 @@ export class FormulaDependencyGenerator extends Disposable {
const otherFormulaData = this._otherFormulaManagerService.getOtherFormulaData();
- const treeList = await this._generateTreeList(formulaData, otherFormulaData);
+ const unitData = this._currentConfigService.getUnitData();
+
+ const treeList = await this._generateTreeList(formulaData, otherFormulaData, unitData);
const updateTreeList = this._getUpdateTreeListAndMakeDependency(treeList);
@@ -137,7 +139,11 @@ export class FormulaDependencyGenerator extends Disposable {
* @param formulaData
* @returns
*/
- private async _generateTreeList(formulaData: IFormulaData, otherFormulaData: IOtherFormulaData) {
+ private async _generateTreeList(
+ formulaData: IFormulaData,
+ otherFormulaData: IOtherFormulaData,
+ unitData: IUnitData
+ ) {
const formulaDataKeys = Object.keys(formulaData);
const otherFormulaDataKeys = Object.keys(otherFormulaData);
@@ -169,6 +175,8 @@ export class FormulaDependencyGenerator extends Disposable {
const FDtree = new FormulaDependencyTree();
+ const sheetItem = unitData[unitId][sheetId];
+
FDtree.node = node;
FDtree.formula = formulaString;
FDtree.unitId = unitId;
@@ -176,6 +184,9 @@ export class FormulaDependencyGenerator extends Disposable {
FDtree.row = row;
FDtree.column = column;
+ FDtree.rowCount = sheetItem.rowCount;
+ FDtree.columnCount = sheetItem.columnCount;
+
treeList.push(FDtree);
});
}
@@ -247,7 +258,14 @@ export class FormulaDependencyGenerator extends Disposable {
for (let i = 0, len = treeList.length; i < len; i++) {
const tree = treeList[i];
- this._runtimeService.setCurrent(tree.row, tree.column, tree.subUnitId, tree.unitId);
+ this._runtimeService.setCurrent(
+ tree.row,
+ tree.column,
+ tree.rowCount,
+ tree.columnCount,
+ tree.subUnitId,
+ tree.unitId
+ );
if (tree.node == null) {
continue;
diff --git a/packages/engine-formula/src/functions/lookup/indirect/__test__/index.spec.ts b/packages/engine-formula/src/functions/lookup/indirect/__test__/index.spec.ts
index 0ad833e7ee..6f7c4ceff5 100644
--- a/packages/engine-formula/src/functions/lookup/indirect/__test__/index.spec.ts
+++ b/packages/engine-formula/src/functions/lookup/indirect/__test__/index.spec.ts
@@ -70,7 +70,16 @@ describe('Test indirect', () => {
},
});
- formulaRuntimeService.setCurrent(0, 0, testBed.sheetId, testBed.unitId);
+ const sheetItem = testBed.sheetData[testBed.sheetId];
+
+ formulaRuntimeService.setCurrent(
+ 0,
+ 0,
+ sheetItem.rowCount,
+ sheetItem.columnCount,
+ testBed.sheetId,
+ testBed.unitId
+ );
functionService.registerExecutors(
new Indirect(FUNCTION_NAMES_LOOKUP.INDIRECT),
diff --git a/packages/engine-formula/src/functions/math/sum/index.ts b/packages/engine-formula/src/functions/math/sum/index.ts
index b1ed34a97d..305890a386 100644
--- a/packages/engine-formula/src/functions/math/sum/index.ts
+++ b/packages/engine-formula/src/functions/math/sum/index.ts
@@ -37,7 +37,7 @@ export class Sum extends BaseFunction {
variant = (variant as ArrayValueObject).sum();
}
- accumulatorAll = accumulatorAll.plus(variant as BaseValueObject);
+ accumulatorAll = accumulatorAll.plus(variant);
}
return accumulatorAll;
diff --git a/packages/engine-formula/src/services/calculate-formula.service.ts b/packages/engine-formula/src/services/calculate-formula.service.ts
index 4977c57eff..88bf077feb 100644
--- a/packages/engine-formula/src/services/calculate-formula.service.ts
+++ b/packages/engine-formula/src/services/calculate-formula.service.ts
@@ -267,7 +267,14 @@ export class CalculateFormulaService extends Disposable {
throw new Error('AstNode or executor is null');
}
- this._runtimeService.setCurrent(tree.row, tree.column, tree.subUnitId, tree.unitId);
+ this._runtimeService.setCurrent(
+ tree.row,
+ tree.column,
+ tree.rowCount,
+ tree.columnCount,
+ tree.subUnitId,
+ tree.unitId
+ );
if (getDirtyData != null && tree.featureId != null) {
/**
diff --git a/packages/engine-formula/src/services/runtime.service.ts b/packages/engine-formula/src/services/runtime.service.ts
index 0146d09219..7a491191a1 100644
--- a/packages/engine-formula/src/services/runtime.service.ts
+++ b/packages/engine-formula/src/services/runtime.service.ts
@@ -94,7 +94,14 @@ export interface IFormulaRuntimeService {
reset(): void;
- setCurrent(row: number, column: number, sheetId: string, unitId: string): void;
+ setCurrent(
+ row: number,
+ column: number,
+ rowCount: number,
+ columnCount: number,
+ sheetId: string,
+ unitId: string
+ ): void;
registerFunctionDefinitionPrivacyVar(lambdaId: string, lambdaVar: Map<string, Nullable<BaseAstNode>>): void;
@@ -168,8 +175,12 @@ export class FormulaRuntimeService extends Disposable implements IFormulaRuntime
private _stopState = false;
- private _currentRow: number = 0;
- private _currentColumn: number = 0;
+ private _currentRow: number = -1;
+ private _currentColumn: number = -1;
+
+ private _currentRowCount: number = -Infinity;
+ private _currentColumnCount: number = -Infinity;
+
private _currentSubUnitId: string = '';
private _currentUnitId: string = '';
@@ -214,6 +225,14 @@ export class FormulaRuntimeService extends Disposable implements IFormulaRuntime
return this._currentColumn;
}
+ get currentRowCount() {
+ return this._currentRowCount;
+ }
+
+ get currentColumnCount() {
+ return this._currentColumnCount;
+ }
+
get currentSubUnitId() {
return this._currentSubUnitId;
}
@@ -326,9 +345,11 @@ export class FormulaRuntimeService extends Disposable implements IFormulaRuntime
this._completedFormulasCount = 0;
}
- setCurrent(row: number, column: number, sheetId: string, unitId: string) {
+ setCurrent(row: number, column: number, rowCount: number, columnCount: number, sheetId: string, unitId: string) {
this._currentRow = row;
this._currentColumn = column;
+ this._currentRowCount = rowCount;
+ this._currentColumnCount = columnCount;
this._currentSubUnitId = sheetId;
this._currentUnitId = unitId;
}
@@ -367,6 +388,8 @@ export class FormulaRuntimeService extends Disposable implements IFormulaRuntime
setRuntimeData(functionVariant: FunctionVariantType) {
const row = this._currentRow;
const column = this._currentColumn;
+ const rowCount = this._currentRowCount;
+ const columnCount = this.currentColumnCount;
const sheetId = this._currentSubUnitId;
const unitId = this._currentUnitId;
@@ -448,7 +471,10 @@ export class FormulaRuntimeService extends Disposable implements IFormulaRuntime
arrayFormulaRange[sheetId] = arrayData.getData();
- if (this._checkIfArrayFormulaRangeHasData(unitId, sheetId, row, column, arrayRange)) {
+ if (
+ this._checkIfArrayFormulaRangeHasData(unitId, sheetId, row, column, arrayRange) ||
+ this._checkIfArrayFormulaExceeded(rowCount, columnCount, arrayRange)
+ ) {
const errorObject = this._objectValueToCellValue(new ErrorValueObject(ErrorType.SPILL));
sheetData.setValue(row, column, errorObject);
clearArrayUnitData.setValue(row, column, errorObject);
@@ -632,6 +658,14 @@ export class FormulaRuntimeService extends Disposable implements IFormulaRuntime
return false;
}
+ private _checkIfArrayFormulaExceeded(rowCount: number, columnCount: number, arrayRange: IRange) {
+ if (arrayRange.endRow >= rowCount || arrayRange.endColumn >= columnCount) {
+ return true;
+ }
+
+ return false;
+ }
+
private _isInDirtyRange(unitId: string, sheetId: string, row: number, column: number) {
const dirtyRanges = this._currentConfigService.getDirtyRanges();
// Handle the deletion and add of sheet.