diff --git a/packages/engine-formula/src/models/formula-data.model.ts b/packages/engine-formula/src/models/formula-data.model.ts index 4bb46ad6f06..efc32f682c1 100644 --- a/packages/engine-formula/src/models/formula-data.model.ts +++ b/packages/engine-formula/src/models/formula-data.model.ts @@ -21,7 +21,6 @@ import type { IFormulaData, IFormulaDataItem, IFormulaIdMap, - IFormulaIdMapData, IRuntimeUnitDataType, ISheetData, IUnitData, @@ -38,8 +37,6 @@ export interface IRangeChange { } export class FormulaDataModel extends Disposable { - private _formulaIdMapData: IFormulaIdMapData = {}; - private _arrayFormulaRange: IArrayFormulaRangeType = {}; private _arrayFormulaCellData: IArrayFormulaUnitCellType = {}; @@ -49,14 +46,12 @@ export class FormulaDataModel extends Disposable { @Inject(LexerTreeBuilder) private readonly _lexerTreeBuilder: LexerTreeBuilder ) { super(); - this._initFormulaIdMap(); } override dispose() { super.dispose(); this._arrayFormulaRange = {}; this._arrayFormulaCellData = {}; - this._formulaIdMapData = {}; } clearPreviousArrayFormulaCellData(clearArrayFormulaCellData: IRuntimeUnitDataType) { @@ -304,22 +299,16 @@ export class FormulaDataModel extends Disposable { updateFormulaData(unitId: string, sheetId: string, cellValue: IObjectMatrixPrimitiveType>) { const cellMatrix = new ObjectMatrix(cellValue); - if (this._formulaIdMapData[unitId] == null) { - this._formulaIdMapData[unitId] = {}; - } - - if (this._formulaIdMapData[unitId][sheetId] == null) { - this._formulaIdMapData[unitId][sheetId] = {}; - } - - const formulaIdMap = this._formulaIdMapData[unitId][sheetId]; // Connect the formula and ID + const formulaIdMap = this._getSheetFormulaIdMap(unitId, sheetId); // Connect the formula and ID const deleteFormulaIdMap = new Map(); const formulaData = this.getFormulaData(); + if (formulaData[unitId] == null) { formulaData[unitId] = {}; } + const workbookFormulaData = formulaData[unitId]!; if (workbookFormulaData[sheetId] == null) { @@ -551,27 +540,6 @@ export class FormulaDataModel extends Disposable { return dirtyRanges; } - private _initFormulaIdMap() { - const allSheets = this._univerInstanceService.getAllUnitsForType(UniverInstanceType.UNIVER_SHEET); - if (allSheets.length === 0) { - return; - } - - allSheets.forEach((workbook) => { - const unitId = workbook.getUnitId(); - this._formulaIdMapData[unitId] = {}; - - const worksheets = workbook.getSheets(); - worksheets.forEach((worksheet) => { - const sheetId = worksheet.getSheetId(); - - if (this._formulaIdMapData[unitId]) { - this._formulaIdMapData[unitId][sheetId] = this._getSheetFormulaIdMap(unitId, sheetId); - } - }); - }); - } - private _getSheetFormulaIdMap(unitId: string, sheetId: string) { const formulaIdMap: Nullable<{ [formulaId: string]: IFormulaIdMap }> = {}; // Connect the formula and ID diff --git a/packages/sheets-formula/src/controllers/update-formula.controller.ts b/packages/sheets-formula/src/controllers/update-formula.controller.ts index 2efe35bca66..ad8e02c4886 100644 --- a/packages/sheets-formula/src/controllers/update-formula.controller.ts +++ b/packages/sheets-formula/src/controllers/update-formula.controller.ts @@ -106,9 +106,21 @@ export class UpdateFormulaController extends Disposable { })); this.disposeWithMe( - this._commandService.onCommandExecuted((command: ICommandInfo, options?: IExecutionOptions) => { + this._commandService.onCommandExecuted((command: ICommandInfo) => { if (!command.params) return; + if (command.id === RemoveSheetMutation.id) { + const { subUnitId: sheetId, unitId } = command.params as IRemoveSheetMutationParams; + this._handleWorkbookDisposed(unitId, sheetId); + } else if (command.id === InsertSheetMutation.id) { + this._handleInsertSheetMutation(command.params as IInsertSheetMutationParams); + } + }) + ); + + // Make sure to get the complete formula history data before updating, which contains the complete mapping of f and si + this.disposeWithMe( + this._commandService.beforeCommandExecuted((command: ICommandInfo, options?: IExecutionOptions) => { if (command.id === SetRangeValuesMutation.id) { const params = command.params as ISetRangeValuesMutationParams; @@ -121,11 +133,6 @@ export class UpdateFormulaController extends Disposable { return; } this._handleSetRangeValuesMutation(params as ISetRangeValuesMutationParams); - } else if (command.id === RemoveSheetMutation.id) { - const { subUnitId: sheetId, unitId } = command.params as IRemoveSheetMutationParams; - this._handleWorkbookDisposed(unitId, sheetId); - } else if (command.id === InsertSheetMutation.id) { - this._handleInsertSheetMutation(command.params as IInsertSheetMutationParams); } }) );