Skip to content

Commit

Permalink
fix(formula): remove formula id map
Browse files Browse the repository at this point in the history
  • Loading branch information
Dushusir committed Nov 29, 2024
1 parent 94b3af9 commit deb5200
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 41 deletions.
38 changes: 3 additions & 35 deletions packages/engine-formula/src/models/formula-data.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import type {
IFormulaData,
IFormulaDataItem,
IFormulaIdMap,
IFormulaIdMapData,
IRuntimeUnitDataType,
ISheetData,
IUnitData,
Expand All @@ -38,8 +37,6 @@ export interface IRangeChange {
}

export class FormulaDataModel extends Disposable {
private _formulaIdMapData: IFormulaIdMapData = {};

private _arrayFormulaRange: IArrayFormulaRangeType = {};

private _arrayFormulaCellData: IArrayFormulaUnitCellType = {};
Expand All @@ -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) {
Expand Down Expand Up @@ -304,22 +299,16 @@ export class FormulaDataModel extends Disposable {
updateFormulaData(unitId: string, sheetId: string, cellValue: IObjectMatrixPrimitiveType<Nullable<ICellData>>) {
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<string, string | IFormulaIdMap>();

const formulaData = this.getFormulaData();

if (formulaData[unitId] == null) {
formulaData[unitId] = {};
}

const workbookFormulaData = formulaData[unitId]!;

if (workbookFormulaData[sheetId] == null) {
Expand Down Expand Up @@ -551,27 +540,6 @@ export class FormulaDataModel extends Disposable {
return dirtyRanges;
}

private _initFormulaIdMap() {
const allSheets = this._univerInstanceService.getAllUnitsForType<Workbook>(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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
})
);
Expand Down

0 comments on commit deb5200

Please sign in to comment.