Skip to content

Commit

Permalink
fix(formula): update formula data model
Browse files Browse the repository at this point in the history
  • Loading branch information
Dushusir committed Nov 29, 2024
1 parent 81cf367 commit 94b3af9
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 301 deletions.
10 changes: 10 additions & 0 deletions packages/engine-formula/src/basics/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ export interface IFormulaData {
[unitId: string]: Nullable<{ [sheetId: string]: Nullable<IObjectMatrixPrimitiveType<Nullable<IFormulaDataItem>>> }>;
}

export interface IFormulaIdMap {
f: string;
r: number;
c: number;
}

export interface IFormulaIdMapData {
[unitId: string]: Nullable<{ [subUnitId: string]: Nullable<{ [formulaId: string]: IFormulaIdMap }> }>;
}

export interface IOtherFormulaData {
[unitId: string]: Nullable<{ [subUnitId: string]: Nullable<{ [formulaId: string]: IOtherFormulaDataItem }> }>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
*/

import type { ICommandInfo } from '@univerjs/core';
import type { IFormulaData } from '../basics/common';

import type { ISetArrayFormulaDataMutationParams } from '../commands/mutations/set-array-formula-data.mutation';
import type { ISetFormulaCalculationStartMutation } from '../commands/mutations/set-formula-calculation.mutation';
import type { ISetFormulaDataMutationParams } from '../commands/mutations/set-formula-data.mutation';
import type { IFormulaDirtyData } from '../services/current-data.service';
import { Disposable, ICommandService, Inject } from '@univerjs/core';
import { convertRuntimeToUnitData } from '../basics/runtime';
Expand All @@ -30,7 +28,6 @@ import {
SetFormulaCalculationStartMutation,
SetFormulaCalculationStopMutation,
} from '../commands/mutations/set-formula-calculation.mutation';
import { SetFormulaDataMutation } from '../commands/mutations/set-formula-data.mutation';
import { FormulaDataModel } from '../models/formula-data.model';
import { ICalculateFormulaService } from '../services/calculate-formula.service';
import { FormulaExecutedStateType, type IAllRuntimeData } from '../services/runtime.service';
Expand All @@ -56,11 +53,6 @@ export class CalculateController extends Disposable {
this._commandService.onCommandExecuted((command: ICommandInfo) => {
if (command.id === SetFormulaCalculationStopMutation.id) {
this._calculateFormulaService.stopFormulaExecution();
} else if (command.id === SetFormulaDataMutation.id) {
const formulaData = (command.params as ISetFormulaDataMutationParams).formulaData as IFormulaData;

// formulaData is the incremental data sent from the main thread and needs to be merged into formulaDataModel
this._formulaDataModel.mergeFormulaData(formulaData);
} else if (command.id === SetFormulaCalculationStartMutation.id) {
const params = command.params as ISetFormulaCalculationStartMutation;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,6 @@ export function createCommandTestBed(workbookData?: IWorkbookData, dependencies?
registerFormulaDependencies(this._injector);
dependencies?.forEach((d) => this._injector.add(d));
}

override onReady(): void {
this._formulaDataModel?.initFormulaData();
}
}

univer.registerPlugin(TestPlugin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,6 @@ export function createFunctionTestBed(workbookData?: IWorkbookData, dependencies

dependencies?.forEach((d) => injector.add(d));
}

override onReady(): void {
this._formulaDataModel?.initFormulaData();
}
}

univer.registerPlugin(TestPlugin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ describe('Test formula data model', () => {

describe('updateFormulaData', () => {
it('delete formula with id', () => {
formulaDataModel.initFormulaData();

const unitId = 'test';
const sheetId = 'sheet1';
const cellValue = {
Expand All @@ -113,41 +111,30 @@ describe('Test formula data model', () => {
};

const result = {
[unitId]: {
[sheetId]: {
0: {
3: {
f: '=SUM(A1)',
si: '3e4r5t',
},
},
2: {
3: {
f: '=SUM(A3)',
si: 'OSPtzm',
},
},
3: {
3: {
f: '=SUM(A3)',
si: 'OSPtzm',
x: 0,
y: 1,
},
},
1: {
3: null,
},
2: {
3: {
f: '=SUM(A3)',
si: 'OSPtzm',
},
},
3: {
3: {
f: '=SUM(A3)',
si: 'OSPtzm',
x: 0,
y: 1,
},
},
};

formulaDataModel.updateFormulaData(unitId, sheetId, cellValue);

const formulaData = formulaDataModel.getFormulaData();
expect(formulaData).toStrictEqual(result);
const newFormulaData = formulaDataModel.updateFormulaData(unitId, sheetId, cellValue);
expect(newFormulaData).toStrictEqual(result);
});

it('delete formulas with ids and formulas with only ids', () => {
formulaDataModel.initFormulaData();

const unitId = 'test';
const sheetId = 'sheet1';
const cellValue = {
Expand Down Expand Up @@ -178,27 +165,28 @@ describe('Test formula data model', () => {
};

const result = {
[unitId]: {
[sheetId]: {
3: {
3: {
f: '=SUM(A4)',
si: 'OSPtzm',
},
},
0: {
3: null,
},
1: {
3: null,
},
2: {
3: null,
},
3: {
3: {
f: '=SUM(A4)',
si: 'OSPtzm',
},
},
};

formulaDataModel.updateFormulaData(unitId, sheetId, cellValue);

const formulaData = formulaDataModel.getFormulaData();
expect(formulaData).toStrictEqual(result);
const newFormulaData = formulaDataModel.updateFormulaData(unitId, sheetId, cellValue);
expect(newFormulaData).toStrictEqual(result);
});

it('delete the formula with only id', () => {
formulaDataModel.initFormulaData();

const unitId = 'test';
const sheetId = 'sheet1';
const cellValue = {
Expand All @@ -213,36 +201,27 @@ describe('Test formula data model', () => {
};

const result = {
[unitId]: {
[sheetId]: {
0: {
3: {
f: '=SUM(A1)',
si: '3e4r5t',
},
},
1: {
3: {
f: '=SUM(A2)',
si: 'OSPtzm',
},
},
2: {
3: {
f: '=SUM(A2)',
si: 'OSPtzm',
x: 0,
y: 1,
},
},
1: {
3: {
f: '=SUM(A2)',
si: 'OSPtzm',
},
},
2: {
3: {
f: '=SUM(A2)',
si: 'OSPtzm',
x: 0,
y: 1,
},
},
3: {
3: null,
},
};

formulaDataModel.updateFormulaData(unitId, sheetId, cellValue);

const formulaData = formulaDataModel.getFormulaData();
expect(formulaData).toStrictEqual(result);
const newFormulaData = formulaDataModel.updateFormulaData(unitId, sheetId, cellValue);
expect(newFormulaData).toStrictEqual(result);
});
});

Expand Down Expand Up @@ -352,8 +331,6 @@ describe('Test formula data model', () => {

describe('getFormulaStringByCell', () => {
it('get formula string by cell', () => {
formulaDataModel.initFormulaData();

const unitId = 'test';
const sheetId = 'sheet1';

Expand Down
Loading

0 comments on commit 94b3af9

Please sign in to comment.