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 the issue of array inverted index cache (#6091)

wpxp123456dd786238a8a5
diff --git a/packages/engine-formula/src/engine/value-object/array-value-object.ts b/packages/engine-formula/src/engine/value-object/array-value-object.ts
index 0c59693438..3587bc2eb7 100644
--- a/packages/engine-formula/src/engine/value-object/array-value-object.ts
+++ b/packages/engine-formula/src/engine/value-object/array-value-object.ts
@@ -1728,6 +1728,14 @@ export class ArrayValueObject extends BaseValueObject {
return;
}
+ /**
+ * The blank cell are not stored in the inverted index, so skip it.
+ * If needed store it in the future. ask @DR-Univer
+ */
+ if (currentValue.isNull()) {
+ return;
+ }
+
if (currentValue.isError()) {
CELL_INVERTED_INDEX_CACHE.set(
unitId,
diff --git a/packages/sheets/src/controllers/__tests__/formula/array-inverted-index-cache4.spec.ts b/packages/sheets/src/controllers/__tests__/formula/array-inverted-index-cache4.spec.ts
new file mode 100644
index 0000000000..8f698b8dad
--- /dev/null
+++ b/packages/sheets/src/controllers/__tests__/formula/array-inverted-index-cache4.spec.ts
@@ -0,0 +1,214 @@
+/**
+ * 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 { CellValue, Ctor, Injector, IWorkbookData, Nullable, Worksheet } from '@univerjs/core';
+import type { BaseFunction, IFunctionNames } from '@univerjs/engine-formula';
+import type { FFormula } from '@univerjs/engine-formula/facade';
+import { ICommandService, LocaleType } from '@univerjs/core';
+import {
+ functionLogical,
+ functionLookup,
+ functionMeta,
+ functionText,
+ IFormulaCurrentConfigService,
+ IFormulaRuntimeService,
+ IFunctionService,
+ SetArrayFormulaDataMutation,
+ SetFormulaCalculationNotificationMutation,
+ SetFormulaCalculationResultMutation,
+ SetFormulaCalculationStartMutation,
+ SetFormulaCalculationStopMutation,
+} from '@univerjs/engine-formula';
+import { beforeEach, describe, expect, it } from 'vitest';
+import { SetRangeValuesMutation } from '../../../commands/mutations/set-range-values.mutation';
+import { createFunctionTestBed } from './create-function-test-bed';
+
+import '@univerjs/engine-formula/facade';
+
+const unitId = 'test';
+const subUnitId = 'sheet1';
+
+const getFunctionsTestWorkbookData = (): IWorkbookData => {
+ return {
+ id: unitId,
+ appVersion: '3.0.0-alpha',
+ sheets: {
+ sheet1: {
+ id: subUnitId,
+ cellData: {
+ 2: {
+ 1: {
+ v: 'Miles',
+ t: 1,
+ },
+ 2: {
+ v: 'Trip #',
+ t: 1,
+ },
+ },
+ 3: {
+ 1: {
+ v: 222,
+ t: 2,
+ },
+ 2: {
+ v: 'Trip #1',
+ t: 1,
+ },
+ },
+ 4: {
+ 1: {
+ v: 151,
+ t: 2,
+ },
+ 2: {
+ v: 'Trip #2',
+ t: 1,
+ },
+ },
+ 6: {
+ 4: {
+ f: '=IF(B8<>"","Trip #"&1+SUBSTITUTE(LOOKUP(2,1/($C$4:$C7<>""),$C$4:$C7),"Trip #",""),"")',
+ t: 1,
+ },
+ },
+ 7: {
+ 1: {
+ v: 139,
+ t: 2,
+ },
+ 4: {
+ f: '=IF(B8<>"","Trip #"&1+SUBSTITUTE(LOOKUP(2,1/($C$4:$C7<>""),$C$4:$C7),"Trip #",""),"")',
+ v: 'Trip #3',
+ t: 1,
+ },
+ },
+ 8: {
+ 1: {
+ v: 202,
+ t: 2,
+ },
+ },
+ 10: {
+ 1: {
+ v: 216,
+ t: 2,
+ },
+ },
+ 11: {
+ 1: {
+ v: 450,
+ t: 2,
+ },
+ },
+ },
+ },
+ },
+ locale: LocaleType.ZH_CN,
+ name: '',
+ sheetOrder: [],
+ styles: {},
+ };
+};
+
+describe('Test inverted index cache', () => {
+ let get: Injector['get'];
+ let worksheet: Worksheet;
+ let formulaEngine: FFormula;
+ let commandService: ICommandService;
+ let getCellValue: (row: number, column: number) => Nullable<CellValue>;
+
+ beforeEach(async () => {
+ const testBed = createFunctionTestBed(getFunctionsTestWorkbookData());
+
+ get = testBed.get;
+ worksheet = testBed.sheet.getSheetBySheetId(subUnitId) as Worksheet;
+ formulaEngine = testBed.api.getFormula() as FFormula;
+
+ commandService = get(ICommandService);
+
+ commandService.registerCommand(SetFormulaCalculationStartMutation);
+ commandService.registerCommand(SetFormulaCalculationStopMutation);
+ commandService.registerCommand(SetFormulaCalculationResultMutation);
+ commandService.registerCommand(SetFormulaCalculationNotificationMutation);
+ commandService.registerCommand(SetArrayFormulaDataMutation);
+ commandService.registerCommand(SetRangeValuesMutation);
+
+ 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
+ );
+
+ const functions = [
+ ...functionMeta,
+ ...functionLogical,
+ ...functionLookup,
+ ...functionText,
+ ]
+ .map((registerObject) => {
+ const Func = registerObject[0] as Ctor<BaseFunction>;
+ const name = registerObject[1] as IFunctionNames;
+
+ return new Func(name);
+ });
+
+ functionService.registerExecutors(
+ ...functions
+ );
+
+ formulaEngine.executeCalculation();
+ await formulaEngine.onCalculationEnd();
+
+ getCellValue = (row: number, column: number) => {
+ return worksheet.getCellRaw(row, column)?.v;
+ };
+ });
+
+ describe('Test formula', () => {
+ it('Multi formula test', () => {
+ expect(getCellValue(6, 4)).toBe('Trip #3');
+ expect(getCellValue(7, 4)).toBe('Trip #3');
+ });
+ });
+});