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(formula): fix formula rows and columns limit (#4052)

wpxp1234569a78db0c4771
diff --git a/packages/engine-formula/src/engine/ast-node/function-node.ts b/packages/engine-formula/src/engine/ast-node/function-node.ts
index 6075c451b0..ccb7fb400c 100644
--- a/packages/engine-formula/src/engine/ast-node/function-node.ts
+++ b/packages/engine-formula/src/engine/ast-node/function-node.ts
@@ -271,6 +271,12 @@ export class FunctionNode extends BaseAstNode {
const { currentUnitId, currentSubUnitId, currentRow, currentColumn } = this._runtimeService;
this._functionExecutor.setRefInfo(currentUnitId, currentSubUnitId, currentRow, currentColumn);
+
+ if (this._functionExecutor.needsSheetRowColumnCount) {
+ const { rowCount, columnCount } = this._currentConfigService.getSheetRowColumnCount(currentUnitId, currentSubUnitId);
+
+ this._functionExecutor.setSheetRowColumnCount(rowCount, columnCount);
+ }
}
private _setRefData(variant: FunctionVariantType) {
diff --git a/packages/engine-formula/src/functions/base-function.ts b/packages/engine-formula/src/functions/base-function.ts
index de3923dd27..f7f20bb5b6 100644
--- a/packages/engine-formula/src/functions/base-function.ts
+++ b/packages/engine-formula/src/functions/base-function.ts
@@ -45,6 +45,8 @@ export class BaseFunction {
private _sheetOrder: string[];
private _sheetNameMap: { [sheetId: string]: string };
protected _formulaDataModel: Nullable<FormulaDataModel>;
+ protected _rowCount: number = -1;
+ protected _columnCount: number = -1;
/**
* Whether the function needs to expand the parameters
@@ -71,6 +73,11 @@ export class BaseFunction {
*/
needsFormulaDataModel: boolean = false;
+ /**
+ * Whether the function needs the number of rows and columns in the sheet
+ */
+ needsSheetRowColumnCount: boolean = false;
+
/**
* Minimum number of parameters
*/
@@ -159,6 +166,11 @@ export class BaseFunction {
this._formulaDataModel = _formulaDataModel;
}
+ setSheetRowColumnCount(rowCount: number, columnCount: number) {
+ this._rowCount = rowCount;
+ this._columnCount = columnCount;
+ }
+
isAsync() {
return false;
}
diff --git a/packages/engine-formula/src/functions/math/randarray/__tests__/index.spec.ts b/packages/engine-formula/src/functions/math/randarray/__tests__/index.spec.ts
index 4414238b96..a7cc362ba7 100644
--- a/packages/engine-formula/src/functions/math/randarray/__tests__/index.spec.ts
+++ b/packages/engine-formula/src/functions/math/randarray/__tests__/index.spec.ts
@@ -14,109 +14,251 @@
* limitations under the License.
*/
-import { describe, expect, it, vi } from 'vitest';
+import type { Injector, IWorkbookData } from '@univerjs/core';
+import type { LexerNode } from '../../../../engine/analysis/lexer-node';
+import type { BaseAstNode } from '../../../../engine/ast-node/base-ast-node';
+import { CellValueType, LocaleType } from '@univerjs/core';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+import { ErrorType } from '../../../../basics/error-type';
+import { Lexer } from '../../../../engine/analysis/lexer';
+import { AstTreeBuilder } from '../../../../engine/analysis/parser';
+import { Interpreter } from '../../../../engine/interpreter/interpreter';
+import { generateExecuteAstNodeData } from '../../../../engine/utils/ast-node-tool';
+import { IFormulaCurrentConfigService } from '../../../../services/current-data.service';
+import { IFunctionService } from '../../../../services/function.service';
+import { IFormulaRuntimeService } from '../../../../services/runtime.service';
+import { createFunctionTestBed, getObjectValue } from '../../../__tests__/create-function-test-bed';
import { FUNCTION_NAMES_MATH } from '../../function-names';
import { Randarray } from '../index';
-import { BooleanValueObject, NullValueObject, NumberValueObject } from '../../../../engine/value-object/primitive-object';
-import { ArrayValueObject, transformToValue } from '../../../../engine/value-object/array-value-object';
-import { ErrorType } from '../../../../basics/error-type';
+
+const getTestWorkbookData = (): IWorkbookData => {
+ return {
+ id: 'test',
+ appVersion: '3.0.0-alpha',
+ sheets: {
+ sheet1: {
+ id: 'sheet1',
+ cellData: {
+ 0: {
+ 0: {
+ v: 1,
+ t: CellValueType.NUMBER,
+ },
+ 1: {
+ v: true,
+ t: CellValueType.BOOLEAN,
+ },
+ 2: {
+ v: false,
+ t: CellValueType.BOOLEAN,
+ },
+ 3: {
+ v: 'test',
+ t: CellValueType.STRING,
+ },
+ 4: {
+ v: null,
+ },
+ 5: {
+ v: ErrorType.NAME,
+ t: CellValueType.STRING,
+ },
+ },
+ 1: {
+ 0: {
+ v: 1,
+ t: CellValueType.NUMBER,
+ },
+ 1: {
+ v: 2,
+ t: CellValueType.NUMBER,
+ },
+ 2: {
+ v: 1,
+ t: CellValueType.NUMBER,
+ },
+ 3: {
+ v: 2.5,
+ t: CellValueType.NUMBER,
+ },
+ },
+ 2: {
+ 0: {
+ v: 2,
+ t: CellValueType.NUMBER,
+ },
+ 1: {
+ v: 3,
+ t: CellValueType.NUMBER,
+ },
+ 2: {
+ v: 2,
+ t: CellValueType.NUMBER,
+ },
+ 3: {
+ v: 3,
+ t: CellValueType.NUMBER,
+ },
+ },
+ 3: {
+ 0: {
+ v: null,
+ },
+ 1: {
+ v: 1,
+ t: CellValueType.NUMBER,
+ },
+ },
+ 4: {
+ 0: {
+ v: null,
+ },
+ 1: {
+ v: null,
+ },
+ },
+ },
+ rowCount: 1000,
+ columnCount: 20,
+ },
+ },
+ locale: LocaleType.ZH_CN,
+ name: '',
+ sheetOrder: [],
+ styles: {},
+ };
+};
describe('Test randarray function', () => {
- const testFunction = new Randarray(FUNCTION_NAMES_MATH.RANDARRAY);
+ let get: Injector['get'];
+ let lexer: Lexer;
+ let astTreeBuilder: AstTreeBuilder;
+ let interpreter: Interpreter;
+ let calculate: (formula: string) => (string | number | boolean | null)[][] | string | number | boolean;
+
+ beforeEach(() => {
+ const testBed = createFunctionTestBed(getTestWorkbookData());
+
+ get = testBed.get;
+
+ lexer = get(Lexer);
+ astTreeBuilder = get(AstTreeBuilder);
+ interpreter = get(Interpreter);
+
+ const functionService = get(IFunctionService);
+
+ const formulaCurrentConfigService = get(IFormulaCurrentConfigService);
+
+ const formulaRuntimeService = get(IFormulaRuntimeService);
+
+ formulaCurrentConfigService.load({
+ formulaData: {},
+ arrayFormulaCellData: {},
+ arrayFormulaRange: {},
+ forceCalculate: false,
+ dirtyRanges: [],
+ dirtyNameMap: {},
+ dirtyDefinedNameMap: {},
+ dirtyUnitFeatureMap: {},
+ dirtyUnitOtherFormulaMap: {},
+ excludedCell: {},
+ allUnitData: {
+ [testBed.unitId]: testBed.sheetData,
+ },
+ });
+
+ const sheetItem = testBed.sheetData[testBed.sheetId];
+
+ formulaRuntimeService.setCurrent(
+ 0,
+ 0,
+ sheetItem.rowCount,
+ sheetItem.columnCount,
+ testBed.sheetId,
+ testBed.unitId
+ );
+
+ functionService.registerExecutors(
+ new Randarray(FUNCTION_NAMES_MATH.RANDARRAY)
+ );
+
+ calculate = (formula: string) => {
+ const lexerNode = lexer.treeBuilder(formula);
+
+ const astNode = astTreeBuilder.parse(lexerNode as LexerNode);
+
+ const result = interpreter.execute(generateExecuteAstNodeData(astNode as BaseAstNode));
+
+ return getObjectValue(result);
+ };
+ });
describe('Randarray', () => {
const mockRandom = vi.spyOn(Math, 'random');
mockRandom.mockReturnValue(0.5);
- it('function is normal', () => {
- const result = testFunction.calculate();
- expect(result.getValue()).toBe(0.5);
+ it('Value is normal', async () => {
+ const result = await calculate('=RANDARRAY()');
+ expect(result).toBe(0.5);
});
- it('rows is number', () => {
- const rows = NumberValueObject.create(3);
- const result = testFunction.calculate(rows);
- expect(transformToValue(result.getArrayValue())).toStrictEqual([
+ it('rows is number', async () => {
+ const result = await calculate('=RANDARRAY(3)');
+ expect(result).toStrictEqual([
[0.5],
[0.5],
[0.5],
]);
});
- it('rows is number, columns is number', () => {
- const rows = NumberValueObject.create(3);
- const columns = NumberValueObject.create(3);
- const result = testFunction.calculate(rows, columns);
- expect(transformToValue(result.getArrayValue())).toStrictEqual([
+ it('rows is number, columns is number', async () => {
+ const result = await calculate('=RANDARRAY(3,3)');
+ expect(result).toStrictEqual([
[0.5, 0.5, 0.5],
[0.5, 0.5, 0.5],
[0.5, 0.5, 0.5],
]);
});
- it('rows or columns is 0/-2/false/blank cell', () => {
- const rows = NumberValueObject.create(0);
- const result = testFunction.calculate(rows);
- expect(result.getValue()).toBe(ErrorType.CALC);
+ it('rows and columns is exceed', async () => {
+ const result = await calculate('=RANDARRAY(10000,10000)');
+ expect(result).toBe(ErrorType.VALUE);
+
+ const result2 = await calculate('=RANDARRAY(2000,3)');
+ expect(result2).toBe(ErrorType.REF);
+ });
- const rows2 = NumberValueObject.create(-2);
- const result2 = testFunction.calculate(rows2);
- expect(result2.getValue()).toBe(ErrorType.VALUE);
+ it('rows or columns is 0/-2/false/blank cell', async () => {
+ const result = await calculate('=RANDARRAY(0)');
+ expect(result).toBe(ErrorType.CALC);
- const rows3 = BooleanValueObject.create(false);
- const result3 = testFunction.calculate(rows3);
- expect(result3.getValue()).toBe(ErrorType.CALC);
+ const result2 = await calculate('=RANDARRAY(-2)');
+ expect(result2).toBe(ErrorType.VALUE);
- const rows4 = NullValueObject.create();
- const result4 = testFunction.calculate(rows4);
- expect(result4.getValue()).toBe(ErrorType.CALC);
+ const result3 = await calculate('=RANDARRAY(C1)');
+ expect(result3).toBe(ErrorType.CALC);
+
+ const result4 = await calculate('=RANDARRAY(E1)');
+ expect(result4).toBe(ErrorType.CALC);
});
- it('Value is array', () => {
- const rows = NumberValueObject.create(6);
- const columns = NumberValueObject.create(6);
- const min = ArrayValueObject.createByArray([
- [1],
- [2],
- ]);
- const max = ArrayValueObject.createByArray([
- [1, 2],
- [2, 3],
- ]);
- const result = testFunction.calculate(rows, columns, min, max);
- expect(transformToValue(result.getArrayValue())).toStrictEqual([
+ it('Value is array', async () => {
+ const result = await calculate('=RANDARRAY(6,6,A2:A3,A2:B3)');
+ expect(result).toStrictEqual([
[1, 1.5],
[2, 2.5],
]);
});
- it('min > max', () => {
- const rows = NumberValueObject.create(3);
- const columns = NumberValueObject.create(3);
- const min = NumberValueObject.create(3);
- const max = NumberValueObject.create(2);
- const result = testFunction.calculate(rows, columns, min, max);
- expect(result.getValue()).toBe(ErrorType.VALUE);
+ it('min > max', async () => {
+ const result = await calculate('=RANDARRAY(3,3,3,2)');
+ expect(result).toBe(ErrorType.VALUE);
});
- it('wholeNumber', () => {
- const rows = NumberValueObject.create(6);
- const columns = NumberValueObject.create(6);
- const min = ArrayValueObject.createByArray([
- [1],
- [2],
- ]);
- const max = ArrayValueObject.createByArray([
- [1, 2.5],
- [2, 3],
- ]);
- const wholeNumber = ArrayValueObject.createByArray([
- [null, 1],
- [null, null],
- ]);
- const result = testFunction.calculate(rows, columns, min, max, wholeNumber);
- expect(transformToValue(result.getArrayValue())).toStrictEqual([
+ it('wholeNumber', async () => {
+ const result = await calculate('=RANDARRAY(6,6,C2:C3,C2:D3,A4:B5)');
+ expect(result).toStrictEqual([
[1, ErrorType.VALUE],
[2, 2.5],
]);
diff --git a/packages/engine-formula/src/functions/math/randarray/index.ts b/packages/engine-formula/src/functions/math/randarray/index.ts
index 8dc8d4158c..2c7eb45823 100644
--- a/packages/engine-formula/src/functions/math/randarray/index.ts
+++ b/packages/engine-formula/src/functions/math/randarray/index.ts
@@ -15,17 +15,19 @@
*/
import { ErrorType } from '../../../basics/error-type';
+import { expandArrayValueObject } from '../../../engine/utils/array-object';
import { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import { type BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';
-import { BaseFunction } from '../../base-function';
import { NumberValueObject } from '../../../engine/value-object/primitive-object';
-import { expandArrayValueObject } from '../../../engine/utils/array-object';
+import { BaseFunction } from '../../base-function';
export class Randarray extends BaseFunction {
override minParams = 0;
override maxParams = 5;
+ override needsSheetRowColumnCount = true;
+
override calculate(rows?: BaseValueObject, columns?: BaseValueObject, min?: BaseValueObject, max?: BaseValueObject, wholeNumber?: BaseValueObject) {
if (rows?.isError()) {
return rows;
@@ -261,12 +263,23 @@ export class Randarray extends BaseFunction {
};
}
- if (rowsValue < 0 || columnsValue < 0) {
+ const maxRows = this._rowCount - this.row;
+ const maxColumns = this._columnCount - this.column;
+
+ // max count of cells is 10^7
+ if (rowsValue < 0 || columnsValue < 0 || rowsValue * columnsValue > 10 ** 7) {
return {
errorObject: ErrorValueObject.create(ErrorType.VALUE),
};
}
+ // Cannot exceed the current number of available rows and columns in the table
+ if (rowsValue > maxRows || columnsValue > maxColumns) {
+ return {
+ errorObject: ErrorValueObject.create(ErrorType.REF),
+ };
+ }
+
const minValue = +minObject.getValue();
const maxValue = +maxObject.getValue();
const wholeNumberValue = +wholeNumberObject.getValue();
diff --git a/packages/engine-formula/src/functions/math/sequence/__tests__/index.spec.ts b/packages/engine-formula/src/functions/math/sequence/__tests__/index.spec.ts
index e87322596b..4efdbe6f4c 100644
--- a/packages/engine-formula/src/functions/math/sequence/__tests__/index.spec.ts
+++ b/packages/engine-formula/src/functions/math/sequence/__tests__/index.spec.ts
@@ -14,27 +14,188 @@
* limitations under the License.
*/
-import { describe, expect, it } from 'vitest';
+import type { Injector, IWorkbookData } from '@univerjs/core';
+import type { LexerNode } from '../../../../engine/analysis/lexer-node';
+import type { BaseAstNode } from '../../../../engine/ast-node/base-ast-node';
+import { CellValueType, LocaleType } from '@univerjs/core';
+import { beforeEach, describe, expect, it } from 'vitest';
import { ErrorType } from '../../../../basics/error-type';
-import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';
-import { ErrorValueObject } from '../../../../engine/value-object/base-value-object';
-import { BooleanValueObject, NullValueObject, NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';
-import { getObjectValue } from '../../../__tests__/create-function-test-bed';
+import { Lexer } from '../../../../engine/analysis/lexer';
+import { AstTreeBuilder } from '../../../../engine/analysis/parser';
+import { Interpreter } from '../../../../engine/interpreter/interpreter';
+import { generateExecuteAstNodeData } from '../../../../engine/utils/ast-node-tool';
+import { IFormulaCurrentConfigService } from '../../../../services/current-data.service';
+import { IFunctionService } from '../../../../services/function.service';
+import { IFormulaRuntimeService } from '../../../../services/runtime.service';
+import { createFunctionTestBed, getObjectValue } from '../../../__tests__/create-function-test-bed';
import { FUNCTION_NAMES_MATH } from '../../function-names';
import { Sequence } from '../index';
+const getTestWorkbookData = (): IWorkbookData => {
+ return {
+ id: 'test',
+ appVersion: '3.0.0-alpha',
+ sheets: {
+ sheet1: {
+ id: 'sheet1',
+ cellData: {
+ 0: {
+ 0: {
+ v: 1,
+ t: CellValueType.NUMBER,
+ },
+ 1: {
+ v: true,
+ t: CellValueType.BOOLEAN,
+ },
+ 2: {
+ v: false,
+ t: CellValueType.BOOLEAN,
+ },
+ 3: {
+ v: 'test',
+ t: CellValueType.STRING,
+ },
+ 4: {
+ v: null,
+ },
+ 5: {
+ v: ErrorType.NAME,
+ t: CellValueType.STRING,
+ },
+ },
+ 1: {
+ 0: {
+ v: 1,
+ t: CellValueType.NUMBER,
+ },
+ 1: {
+ v: ' ',
+ t: CellValueType.STRING,
+ },
+ 2: {
+ v: 1.23,
+ t: CellValueType.NUMBER,
+ },
+ 3: {
+ v: true,
+ t: CellValueType.BOOLEAN,
+ },
+ 4: {
+ v: false,
+ t: CellValueType.BOOLEAN,
+ },
+ 5: {
+ v: null,
+ },
+ },
+ 2: {
+ 0: {
+ v: 0,
+ t: CellValueType.NUMBER,
+ },
+ 1: {
+ v: '100',
+ t: CellValueType.STRING,
+ },
+ 2: {
+ v: '2.34',
+ t: CellValueType.STRING,
+ },
+ 3: {
+ v: 'test',
+ t: CellValueType.STRING,
+ },
+ 4: {
+ v: -3,
+ t: CellValueType.NUMBER,
+ },
+ 5: {
+ v: ErrorType.NAME,
+ t: CellValueType.STRING,
+ },
+ },
+ },
+ rowCount: 1000,
+ columnCount: 20,
+ },
+ },
+ locale: LocaleType.ZH_CN,
+ name: '',
+ sheetOrder: [],
+ styles: {},
+ };
+};
+
describe('Test sequence function', () => {
- const testFunction = new Sequence(FUNCTION_NAMES_MATH.SEQUENCE);
+ let get: Injector['get'];
+ let lexer: Lexer;
+ let astTreeBuilder: AstTreeBuilder;
+ let interpreter: Interpreter;
+ let calculate: (formula: string) => (string | number | boolean | null)[][] | string | number | boolean;
+
+ beforeEach(() => {
+ const testBed = createFunctionTestBed(getTestWorkbookData());
+
+ get = testBed.get;
+
+ lexer = get(Lexer);
+ astTreeBuilder = get(AstTreeBuilder);
+ interpreter = get(Interpreter);
+
+ const functionService = get(IFunctionService);
+
+ const formulaCurrentConfigService = get(IFormulaCurrentConfigService);
+
+ const formulaRuntimeService = get(IFormulaRuntimeService);
+
+ formulaCurrentConfigService.load({
+ formulaData: {},
+ arrayFormulaCellData: {},
+ arrayFormulaRange: {},
+ forceCalculate: false,
+ dirtyRanges: [],
+ dirtyNameMap: {},
+ dirtyDefinedNameMap: {},
+ dirtyUnitFeatureMap: {},
+ dirtyUnitOtherFormulaMap: {},
+ excludedCell: {},
+ allUnitData: {
+ [testBed.unitId]: testBed.sheetData,
+ },
+ });
+
+ const sheetItem = testBed.sheetData[testBed.sheetId];
+
+ formulaRuntimeService.setCurrent(
+ 0,
+ 0,
+ sheetItem.rowCount,
+ sheetItem.columnCount,
+ testBed.sheetId,
+ testBed.unitId
+ );
+
+ functionService.registerExecutors(
+ new Sequence(FUNCTION_NAMES_MATH.SEQUENCE)
+ );
+
+ calculate = (formula: string) => {
+ const lexerNode = lexer.treeBuilder(formula);
+
+ const astNode = astTreeBuilder.parse(lexerNode as LexerNode);
+
+ const result = interpreter.execute(generateExecuteAstNodeData(astNode as BaseAstNode));
+
+ return getObjectValue(result);
+ };
+ });
describe('Sequence', () => {
- it('Value is normal number', () => {
- const rows = NumberValueObject.create(4);
- const columns = NumberValueObject.create(5);
- const start = NumberValueObject.create(2);
- const step = NumberValueObject.create(2);
- const result = testFunction.calculate(rows, columns, start, step);
- expect(getObjectValue(result)).toStrictEqual([
+ it('Value is normal', async () => {
+ const result = await calculate('=SEQUENCE(4,5,2,2)');
+ expect(result).toStrictEqual([
[2, 4, 6, 8, 10],
[12, 14, 16, 18, 20],
[22, 24, 26, 28, 30],
@@ -42,116 +203,67 @@ describe('Test sequence function', () => {
]);
});
- it('Value is number negative', () => {
- const rows = NumberValueObject.create(-1);
- const columns = NumberValueObject.create(5);
- const start = NumberValueObject.create(2);
- const step = NumberValueObject.create(2);
- const result = testFunction.calculate(rows, columns, start, step);
- expect(getObjectValue(result)).toStrictEqual(ErrorType.VALUE);
+ it('rows and columns is exceed', async () => {
+ const result = await calculate('=SEQUENCE(10000,10000)');
+ expect(result).toBe(ErrorType.VALUE);
+
+ const result2 = await calculate('=SEQUENCE(2000,3)');
+ expect(result2).toBe(ErrorType.REF);
});
- it('Value is number string', () => {
- const rows = StringValueObject.create('1.5');
- const columns = NumberValueObject.create(5);
- const start = NumberValueObject.create(2);
- const step = NumberValueObject.create(2);
- const result = testFunction.calculate(rows, columns, start, step);
- expect(getObjectValue(result)).toStrictEqual([
+ it('Value is number negative', async () => {
+ const result = await calculate('=SEQUENCE(-1,5,2,2)');
+ expect(result).toBe(ErrorType.VALUE);
+ });
+
+ it('Value is number string', async () => {
+ const result = await calculate('=SEQUENCE("1.5",5,2,2)');
+ expect(result).toStrictEqual([
[2, 4, 6, 8, 10],
]);
});
- it('Value is normal string', () => {
- const rows = StringValueObject.create('test');
- const columns = NumberValueObject.create(5);
- const start = NumberValueObject.create(2);
- const step = NumberValueObject.create(2);
- const result = testFunction.calculate(rows, columns, start, step);
- expect(getObjectValue(result)).toStrictEqual(ErrorType.VALUE);
+ it('Value is normal string', async () => {
+ const result = await calculate('=SEQUENCE("test",5,2,2)');
+ expect(result).toBe(ErrorType.VALUE);
});
- it('Value is boolean', () => {
- const rows = BooleanValueObject.create(true);
- const columns = NumberValueObject.create(5);
- const start = NumberValueObject.create(2);
- const step = NumberValueObject.create(2);
- const result = testFunction.calculate(rows, columns, start, step);
- expect(getObjectValue(result)).toStrictEqual([
+ it('Value is boolean', async () => {
+ const result = await calculate('=SEQUENCE(B1,5,2,2)');
+ expect(result).toStrictEqual([
[2, 4, 6, 8, 10],
]);
});
- it('Value is blank cell', () => {
- const rows = NumberValueObject.create(4);
- const columns = ArrayValueObject.create({
- calculateValueList: transformToValueObject([
- [null],
- ]),
- rowCount: 1,
- columnCount: 1,
- unitId: '',
- sheetId: '',
- row: 0,
- column: 0,
- });
- const start = NumberValueObject.create(2);
- const step = NumberValueObject.create(2);
- const result = testFunction.calculate(rows, columns, start, step);
- expect(getObjectValue(result)).toStrictEqual(ErrorType.CALC);
+ it('Value is blank cell', async () => {
+ const result = await calculate('=SEQUENCE(4,E1,2,2)');
+ expect(result).toBe(ErrorType.CALC);
});
- it('Value is null', () => {
- const rows = NullValueObject.create();
- const columns = NullValueObject.create();
- const start = NullValueObject.create();
- const step = NullValueObject.create();
- const result = testFunction.calculate(rows, columns, start, step);
- expect(getObjectValue(result)).toStrictEqual([
+ it('Value is null', async () => {
+ const result = await calculate('=SEQUENCE(,)');
+ expect(result).toStrictEqual([
[1],
]);
});
- it('Value is error', () => {
- const rows = ErrorValueObject.create(ErrorType.NAME);
- const columns = NumberValueObject.create(5);
- const start = NumberValueObject.create(2);
- const step = NumberValueObject.create(2);
- const result = testFunction.calculate(rows, columns, start, step);
- expect(getObjectValue(result)).toStrictEqual(ErrorType.NAME);
-
- const rows2 = NumberValueObject.create(4);
- const columns2 = ErrorValueObject.create(ErrorType.NAME);
- const result2 = testFunction.calculate(rows2, columns2, start, step);
- expect(getObjectValue(result2)).toStrictEqual(ErrorType.NAME);
-
- const start2 = ErrorValueObject.create(ErrorType.NAME);
- const result3 = testFunction.calculate(rows2, columns, start2, step);
- expect(getObjectValue(result3)).toStrictEqual(ErrorType.NAME);
-
- const step2 = ErrorValueObject.create(ErrorType.NAME);
- const result4 = testFunction.calculate(rows2, columns, start, step2);
- expect(getObjectValue(result4)).toStrictEqual(ErrorType.NAME);
+ it('Value is error', async () => {
+ const result = await calculate('=SEQUENCE(F1,5,2,2)');
+ expect(result).toBe(ErrorType.NAME);
+
+ const result2 = await calculate('=SEQUENCE(4,F1,2,2)');
+ expect(result2).toBe(ErrorType.NAME);
+
+ const result3 = await calculate('=SEQUENCE(4,5,F1,2)');
+ expect(result3).toBe(ErrorType.NAME);
+
+ const result4 = await calculate('=SEQUENCE(4,5,2,F1)');
+ expect(result4).toBe(ErrorType.NAME);
});
- it('Value is array', () => {
- const rows = ArrayValueObject.create({
- calculateValueList: transformToValueObject([
- [1, ' ', 1.23, true, false, null],
- [0, '100', '2.34', 'test', -3, ErrorType.NAME],
- ]),
- rowCount: 2,
- columnCount: 6,
- unitId: '',
- sheetId: '',
- row: 0,
- column: 0,
- });
- const columns = NumberValueObject.create(5);
- const start = NumberValueObject.create(2);
- const step = NumberValueObject.create(2);
- const result = testFunction.calculate(rows, columns, start, step);
- expect(getObjectValue(result)).toStrictEqual([
+ it('Value is array', async () => {
+ const result = await calculate('=SEQUENCE(A2:F3,5,2,2)');
+ expect(result).toStrictEqual([
[2, ErrorType.VALUE, 2, 2, ErrorType.CALC, ErrorType.CALC],
[ErrorType.CALC, 2, 2, ErrorType.VALUE, ErrorType.VALUE, ErrorType.NAME],
]);
diff --git a/packages/engine-formula/src/functions/math/sequence/index.ts b/packages/engine-formula/src/functions/math/sequence/index.ts
index 9f352fb4da..2e59cc9026 100644
--- a/packages/engine-formula/src/functions/math/sequence/index.ts
+++ b/packages/engine-formula/src/functions/math/sequence/index.ts
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { ErrorType } from '../../../basics/error-type';
import { expandArrayValueObject } from '../../../engine/utils/array-object';
import { checkVariantsErrorIsStringToNumber } from '../../../engine/utils/check-variant-error';
@@ -21,13 +22,14 @@ import { ArrayValueObject } from '../../../engine/value-object/array-value-objec
import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
export class Sequence extends BaseFunction {
override minParams = 1;
override maxParams = 4;
+ override needsSheetRowColumnCount = true;
+
override calculate(rows: BaseValueObject, columns?: BaseValueObject, start?: BaseValueObject, step?: BaseValueObject): BaseValueObject {
let _rows = rows;
let _columns = columns ?? NumberValueObject.create(1);
@@ -121,7 +123,8 @@ export class Sequence extends BaseFunction {
const startValue = +_startObject.getValue();
const stepValue = +_stepObject.getValue();
- if (rowsValue < 0 || columnsValue < 0) {
+ // max count of cells is 10^7
+ if (rowsValue < 0 || columnsValue < 0 || rowsValue * columnsValue > 10 ** 7) {
return ErrorValueObject.create(ErrorType.VALUE);
}
@@ -129,6 +132,13 @@ export class Sequence extends BaseFunction {
return ErrorValueObject.create(ErrorType.CALC);
}
+ const maxRows = this._rowCount - this.row;
+ const maxColumns = this._columnCount - this.column;
+
+ if (rowsValue > maxRows || columnsValue > maxColumns) {
+ return ErrorValueObject.create(ErrorType.REF);
+ }
+
const result: number[][] = [];
for (let r = 0; r < rowsValue; r++) {
diff --git a/packages/engine-formula/src/services/current-data.service.ts b/packages/engine-formula/src/services/current-data.service.ts
index 120f55c9fb..30e02a616c 100644
--- a/packages/engine-formula/src/services/current-data.service.ts
+++ b/packages/engine-formula/src/services/current-data.service.ts
@@ -103,6 +103,8 @@ export interface IFormulaCurrentConfigService {
sheetOrder: string[];
sheetNameMap: { [sheetId: string]: string };
};
+
+ getSheetRowColumnCount(unitId: string, sheetId: string): { rowCount: number; columnCount: number };
}
export class FormulaCurrentConfigService extends Disposable implements IFormulaCurrentConfigService {
@@ -256,6 +258,20 @@ export class FormulaCurrentConfigService extends Disposable implements IFormulaC
};
}
+ getSheetRowColumnCount(unitId: string, sheetId: string) {
+ const workbook = this._univerInstanceService.getUnit<Workbook>(unitId);
+ const worksheet = workbook?.getSheetBySheetId(sheetId);
+ const snapshot = worksheet?.getSnapshot();
+
+ if (!snapshot) {
+ return { rowCount: 0, columnCount: 0 };
+ }
+
+ const { rowCount, columnCount } = snapshot;
+
+ return { rowCount, columnCount };
+ }
+
load(config: IFormulaDatasetConfig) {
if (config.allUnitData && config.unitSheetNameMap && config.unitStylesData) {
this._unitData = config.allUnitData;