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): worker error (#1565)

Univer47377ae958bf
diff --git a/packages/engine-formula/src/basics/common.ts b/packages/engine-formula/src/basics/common.ts
index e0251ad62e..3428000b96 100644
--- a/packages/engine-formula/src/basics/common.ts
+++ b/packages/engine-formula/src/basics/common.ts
@@ -69,6 +69,10 @@ export interface IRuntimeUnitDataType {
[unitId: string]: Nullable<{ [sheetId: string]: ObjectMatrix<Nullable<ICellData>> }>;
}
+export interface IRuntimeUnitDataPrimitiveType {
+ [unitId: string]: Nullable<{ [sheetId: string]: IObjectMatrixPrimitiveType<Nullable<ICellData>> }>;
+}
+
export interface IRuntimeOtherUnitDataType {
[unitId: string]: Nullable<{ [sheetId: string]: Nullable<{ [formulaId: string]: ICellData }> }>;
}
@@ -93,9 +97,7 @@ export interface IFeatureDirtyRangeType {
[unitId: string]: Nullable<{ [sheetId: string]: IRange[] }>;
}
-export interface IArrayFormulaUnitCellType {
- [unitId: string]: Nullable<{ [sheetId: string]: IObjectMatrixPrimitiveType<Nullable<ICellData>> }>;
-}
+export interface IArrayFormulaUnitCellType extends IRuntimeUnitDataPrimitiveType {}
export interface IFormulaData {
[unitId: string]: Nullable<{ [sheetId: string]: IObjectMatrixPrimitiveType<IFormulaDataItem> }>;
diff --git a/packages/engine-formula/src/basics/runtime.ts b/packages/engine-formula/src/basics/runtime.ts
index e1ec434196..210d46acce 100644
--- a/packages/engine-formula/src/basics/runtime.ts
+++ b/packages/engine-formula/src/basics/runtime.ts
@@ -16,7 +16,7 @@
import { ObjectMatrix } from '@univerjs/core';
-import type { IArrayFormulaUnitCellType, IRuntimeUnitDataType } from './common';
+import type { IArrayFormulaUnitCellType, IRuntimeUnitDataPrimitiveType, IRuntimeUnitDataType } from './common';
export function convertUnitDataToRuntime(unitData: IArrayFormulaUnitCellType) {
const arrayFormulaCellData: IRuntimeUnitDataType = {};
@@ -40,3 +40,26 @@ export function convertUnitDataToRuntime(unitData: IArrayFormulaUnitCellType) {
return arrayFormulaCellData;
}
+
+export function convertRuntimeToUnitData(unitData: IRuntimeUnitDataType) {
+ const unitPrimitiveData: IRuntimeUnitDataPrimitiveType = {};
+ Object.keys(unitData).forEach((unitId) => {
+ const sheetData = unitData[unitId];
+
+ if (sheetData == null) {
+ return true;
+ }
+
+ if (unitPrimitiveData[unitId] == null) {
+ unitPrimitiveData[unitId] = {};
+ }
+
+ Object.keys(sheetData).forEach((sheetId) => {
+ const cellData = sheetData[sheetId];
+
+ unitPrimitiveData[unitId]![sheetId] = cellData.getData();
+ });
+ });
+
+ return unitPrimitiveData;
+}
diff --git a/packages/engine-formula/src/commands/mutations/set-formula-calculation.mutation.ts b/packages/engine-formula/src/commands/mutations/set-formula-calculation.mutation.ts
index 64bc1021ad..8d7d25195b 100644
--- a/packages/engine-formula/src/commands/mutations/set-formula-calculation.mutation.ts
+++ b/packages/engine-formula/src/commands/mutations/set-formula-calculation.mutation.ts
@@ -22,7 +22,7 @@ import type {
IDirtyUnitSheetNameMap,
INumfmtItemMap,
IRuntimeOtherUnitDataType,
- IRuntimeUnitDataType,
+ IRuntimeUnitDataPrimitiveType,
} from '../../basics/common';
import type { FormulaExecutedStateType, IExecutionInProgressParams } from '../../services/runtime.service';
@@ -64,7 +64,7 @@ export const SetFormulaCalculationNotificationMutation: IMutation<ISetFormulaCal
};
export interface ISetFormulaCalculationResultMutation {
- unitData: IRuntimeUnitDataType;
+ unitData: IRuntimeUnitDataPrimitiveType;
unitOtherData: IRuntimeOtherUnitDataType;
}
diff --git a/packages/engine-formula/src/controller/calculate.controller.ts b/packages/engine-formula/src/controller/calculate.controller.ts
index e680ba1354..7dc31f3591 100644
--- a/packages/engine-formula/src/controller/calculate.controller.ts
+++ b/packages/engine-formula/src/controller/calculate.controller.ts
@@ -35,6 +35,7 @@ import { CalculateFormulaService } from '../services/calculate-formula.service';
import type { IAllRuntimeData } from '../services/runtime.service';
import { FormulaExecutedStateType } from '../services/runtime.service';
import { SetNumfmtFormulaDataMutation } from '../commands/mutations/set-numfmt-formula-data.mutation';
+import { convertRuntimeToUnitData } from '../basics/runtime';
@OnLifecycle(LifecycleStages.Ready, CalculateController)
export class CalculateController extends Disposable {
@@ -234,7 +235,7 @@ export class CalculateController extends Disposable {
this._commandService.executeCommand(
SetFormulaCalculationResultMutation.id,
{
- unitData,
+ unitData: convertRuntimeToUnitData(unitData),
unitOtherData,
},
{
diff --git a/packages/sheets/src/controllers/calculate-result-apply.controller.ts b/packages/sheets/src/controllers/calculate-result-apply.controller.ts
index 0d5475aad4..23f4ed0385 100644
--- a/packages/sheets/src/controllers/calculate-result-apply.controller.ts
+++ b/packages/sheets/src/controllers/calculate-result-apply.controller.ts
@@ -70,7 +70,7 @@ export class CalculateResultApplyController extends Disposable {
const setRangeValuesMutation = {
subUnitId: sheetId,
unitId,
- cellValue: cellData.getData(),
+ cellValue: cellData,
};
redoMutationsInfo.push({