Skip to content

Commit

Permalink
fix(formula): use ref range formula (#1694)
Browse files Browse the repository at this point in the history
* fix(formula): use ref range formula

* fix(formula): update formula offset insert column

* fix(formula): undo exchange position

* fix(formula): offset formula move range,column,row

* fix(sheet): ref range command use preUndos and preRedos

* fix(formula): set name, remove sheet preundos

* fix(formula): update formula model after insert sheet and remove sheet

* fix(formula): test update formula

* fix(formula): return blank array after no change formula

* fix(formula): add test cases for update formula

* test(formula): add insert,remove test cases for formula update

* fix(formula): formula update reference

* docs(formula): add formulas i18n,algorithm contributing guide
  • Loading branch information
Dushusir authored Apr 9, 2024
1 parent 03f4668 commit d8f1dc4
Show file tree
Hide file tree
Showing 25 changed files with 1,780 additions and 511 deletions.
26 changes: 25 additions & 1 deletion packages/core/src/shared/__test__/common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,34 @@
*/

import { describe, expect, it } from 'vitest';
import { cellToRange } from '../common';
import { cellToRange, isFormulaId, isFormulaString } from '../common';

describe('Test common', () => {
it('Test cellToRange', () => {
expect(cellToRange(0, 1)).toStrictEqual({ startRow: 0, startColumn: 1, endRow: 0, endColumn: 1 });
});

it('Test isFormulaString', () => {
expect(isFormulaString('=SUM(1)')).toBe(true);
expect(isFormulaString('SUM(1)')).toBe(false);
expect(isFormulaString('=')).toBe(false);
expect(isFormulaString('')).toBe(false);
expect(isFormulaString(1)).toBe(false);
expect(isFormulaString(null)).toBe(false);
expect(isFormulaString(undefined)).toBe(false);
expect(isFormulaString(true)).toBe(false);
expect(isFormulaString({})).toBe(false);
expect(isFormulaString({ f: '' })).toBe(false);
});

it('Test isFormulaId', () => {
expect(isFormulaId('id1')).toBe(true);
expect(isFormulaId('')).toBe(false);
expect(isFormulaId(1)).toBe(false);
expect(isFormulaId(null)).toBe(false);
expect(isFormulaId(undefined)).toBe(false);
expect(isFormulaId(true)).toBe(false);
expect(isFormulaId({})).toBe(false);
expect(isFormulaId({ f: '' })).toBe(false);
});
});
12 changes: 11 additions & 1 deletion packages/core/src/shared/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,22 @@ export function getColorStyle(color: Nullable<IColorStyle>): Nullable<string> {
return null;
}

/**
* A string starting with an equal sign is a formula
* @param value
* @returns
*/
export function isFormulaString(value: any): boolean {
return Tools.isString(value) && value.substring(0, 1) === '=' && value.length > 1;
}

/**
* any string
* @param value
* @returns
*/
export function isFormulaId(value: any): boolean {
return Tools.isString(value) && value.indexOf('=') === -1 && value.length === 6;
return Tools.isString(value) && value.length > 0;
}

/**
Expand Down
3 changes: 0 additions & 3 deletions packages/core/src/types/interfaces/i-range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,6 @@ export interface IOptionData {
*
*/
contentsOnly?: boolean;
/**
* Whether to clear only the comments.
*/
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,23 @@
*/

import type { IMutation } from '@univerjs/core';
import { CommandType, Tools } from '@univerjs/core';
import { CommandType } from '@univerjs/core';
import type { IAccessor } from '@wendellhu/redi';

import type { IArrayFormulaRangeType, IArrayFormulaUnitCellType } from '../../basics/common';
import { FormulaDataModel } from '../../models/formula-data.model';

export interface ISetArrayFormulaDataMutationParams {
arrayFormulaRange: IArrayFormulaRangeType;
arrayFormulaCellData: IArrayFormulaUnitCellType;
}

export const SetArrayFormulaDataUndoMutationFactory = (accessor: IAccessor): ISetArrayFormulaDataMutationParams => {
const formulaDataModel = accessor.get(FormulaDataModel);
const arrayFormulaRange = Tools.deepClone(formulaDataModel.getArrayFormulaRange());
const arrayFormulaCellData = Tools.deepClone(formulaDataModel.getArrayFormulaCellData());
return {
arrayFormulaRange,
arrayFormulaCellData,
};
};

/**
* There is no need to process data here, it is used as the main thread to send data to the worker. The main thread has already updated the data in advance, and there is no need to update it again here.
*/
export const SetArrayFormulaDataMutation: IMutation<ISetArrayFormulaDataMutationParams> = {
id: 'formula.mutation.set-array-formula-data',
type: CommandType.MUTATION,
handler: (accessor: IAccessor, params: ISetArrayFormulaDataMutationParams) => {
const formulaDataModel = accessor.get(FormulaDataModel);
formulaDataModel.setArrayFormulaRange(params.arrayFormulaRange);
formulaDataModel.setArrayFormulaCellData(params.arrayFormulaCellData);
return true;
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ import { CommandType } from '@univerjs/core';
import type { IAccessor } from '@wendellhu/redi';

import type { IFormulaData } from '../../basics/common';
import { FormulaDataModel } from '../../models/formula-data.model';

export interface ISetFormulaDataMutationParams {
formulaData: IFormulaData;
}

/**
* There is no need to process data here, it is used as the main thread to send data to the worker. The main thread has already updated the data in advance, and there is no need to update it again here.
*/
export const SetFormulaDataMutation: IMutation<ISetFormulaDataMutationParams> = {
id: 'formula.mutation.set-formula-data',
type: CommandType.MUTATION,
handler: (accessor: IAccessor, params: ISetFormulaDataMutationParams) => {
const formulaDataModel = accessor.get(FormulaDataModel);
formulaDataModel.setFormulaData(params.formulaData);
return true;
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ export class CalculateController extends Disposable {
this._calculateFormulaService.stopFormulaExecution();
} else if (command.id === SetFormulaDataMutation.id) {
const formulaData = (command.params as ISetFormulaDataMutationParams).formulaData as IFormulaData;
this._formulaDataModel.setFormulaData(formulaData);

// 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 All @@ -83,6 +85,7 @@ export class CalculateController extends Disposable {
}

const { arrayFormulaRange, arrayFormulaCellData } = params;
// TODO@Dushusir: Merge the array formula data into the formulaDataModel
this._formulaDataModel.setArrayFormulaRange(arrayFormulaRange);
this._formulaDataModel.setArrayFormulaCellData(arrayFormulaCellData);
}
Expand Down Expand Up @@ -114,8 +117,6 @@ export class CalculateController extends Disposable {

const arrayFormulaCellData = this._formulaDataModel.getArrayFormulaCellData();

// Synchronous to the main thread
// this._commandService.executeCommand(SetFormulaDataMutation.id, { formulaData });
this._calculateFormulaService.execute({
formulaData,
arrayFormulaCellData,
Expand Down
3 changes: 2 additions & 1 deletion packages/engine-formula/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export { RegisterFunctionMutation } from './commands/mutations/register-function
export {
type ISetArrayFormulaDataMutationParams,
SetArrayFormulaDataMutation,
SetArrayFormulaDataUndoMutationFactory,
} from './commands/mutations/set-array-formula-data.mutation';

export { RemoveDefinedNameMutation, SetDefinedNameMutation, type ISetDefinedNameMutationSearchParam, type ISetDefinedNameMutationParam } from './commands/mutations/set-defined-name.mutation';
Expand Down Expand Up @@ -147,3 +146,5 @@ export { IFormulaRuntimeService, FormulaRuntimeService } from './services/runtim
export { IFormulaCurrentConfigService, FormulaCurrentConfigService } from './services/current-data.service';

export { IActiveDirtyManagerService } from './services/active-dirty-manager.service';

export type { IRangeChange } from './models/formula-data.model';
Loading

0 comments on commit d8f1dc4

Please sign in to comment.