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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(formula): fix the calculation error of formula range absolute reference type moving offset (#5349)
wpxp1234568e8e59c6ae67
diff --git a/packages/core/src/shared/rectangle.ts b/packages/core/src/shared/rectangle.tsindex c239176b66..0ab08f57ee 100644--- a/packages/core/src/shared/rectangle.ts+++ b/packages/core/src/shared/rectangle.ts@@ -458,14 +458,53 @@ export class Rectangle {* // moved = { startRow: 3, startColumn: 2, endRow: 4, endColumn: 3 }* ```*/- static moveOffset = (range: IRange, offsetX: number, offsetY: number): IRange =>- ({- ...range,- startRow: range.startRow + offsetY,- endRow: range.endRow + offsetY,- startColumn: range.startColumn + offsetX,- endColumn: range.endColumn + offsetX,- });+ static moveOffset = (range: IRange, offsetX: number, offsetY: number): IRange => {+ const _range = { ...range };++ // The start absolute reference type determines whether the range start position needs to be moved+ switch (range.startAbsoluteRefType) {+ // The start absolute reference type is row absolute reference, then only the start column position is moved+ case AbsoluteRefType.ROW:+ _range.startColumn += offsetX;+ break;+ // The start absolute reference type is column absolute reference, then only the start row position is moved+ case AbsoluteRefType.COLUMN:+ _range.startRow += offsetY;+ break;+ // The start absolute reference type is both row and column absolute reference, then the start position is not moved+ case AbsoluteRefType.ALL:+ break;+ // The start absolute reference type is not absolute reference, then both the start row and start column positions are moved+ case AbsoluteRefType.NONE:+ default:+ _range.startRow += offsetY;+ _range.startColumn += offsetX;+ break;+ }++ // The end absolute reference type determines whether the range end position needs to be moved+ switch (range.endAbsoluteRefType) {+ // The end absolute reference type is row absolute reference, then only the end column position is moved+ case AbsoluteRefType.ROW:+ _range.endColumn += offsetX;+ break;+ // The end absolute reference type is column absolute reference, then only the end row position is moved+ case AbsoluteRefType.COLUMN:+ _range.endRow += offsetY;+ break;+ // The end absolute reference type is both row and column absolute reference, then the end position is not moved+ case AbsoluteRefType.ALL:+ break;+ // The end absolute reference type is not absolute reference, then both the end row and end column positions are moved+ case AbsoluteRefType.NONE:+ default:+ _range.endRow += offsetY;+ _range.endColumn += offsetX;+ break;+ }++ return _range;+ };/*** Subtracts one range from another, returning the remaining areas as separate rangesdiff --git a/packages/sheets-formula-ui/src/controllers/__tests__/update-formula.controller.spec.ts b/packages/sheets-formula-ui/src/controllers/__tests__/update-formula.controller.spec.tsindex 3c85b1c905..391e53bea4 100644--- a/packages/sheets-formula-ui/src/controllers/__tests__/update-formula.controller.spec.ts+++ b/packages/sheets-formula-ui/src/controllers/__tests__/update-formula.controller.spec.ts@@ -348,7 +348,37 @@ const TEST_WORKBOOK_DATA_DEMO = (): IWorkbookData => ({},name: 'Sheet4',},-+ sheet5: {+ id: 'sheet5',+ name: 'Sheet5',+ cellData: {+ 1: {+ 1: { // B2+ v: 5,+ t: CellValueType.NUMBER,+ },+ 3: { // D2+ f: '=$B2',+ },+ 4: { // E2+ f: '=$B2',+ si: 'MFYkBI',+ },+ 5: { // F2+ si: 'MFYkBI',+ },+ 6: { // G2+ si: 'MFYkBI',+ },+ 7: { // H2+ si: 'MFYkBI',+ },+ 8: { // I2+ si: 'MFYkBI',+ },+ },+ },+ },},locale: LocaleType.ZH_CN,name: '',@@ -917,6 +947,81 @@ describe('Test update formula ', () => {expect(valuesRedo).toStrictEqual([[{ v: 1, t: CellValueType.NUMBER }], [{ f: '=SUM(A8)' }], [{ f: '=SUM(A10)' }], [{ f: '=SUM(A11)' }]]);});+ it('Insert row, update range of absolute reference type', async () => {+ const result = await commandService.executeCommand(InsertRowCommand.id, {+ unitId: 'test',+ subUnitId: 'sheet5',+ range: {+ startRow: 0,+ endRow: 0,+ startColumn: 0,+ endColumn: 19,+ },+ direction: Direction.UP,+ });+ expect(result).toBeTruthy();++ const cellB3 = getValues(2, 1, 2, 1, 'sheet5');+ expect(cellB3).toStrictEqual([+ [{ v: 5, t: CellValueType.NUMBER }],+ ]);+ const cellD3 = getValues(2, 3, 2, 3, 'sheet5');+ expect(cellD3).toStrictEqual([+ [{ f: '=$B3' }],+ ]);+ const cellE3 = getValues(2, 4, 2, 4, 'sheet5');+ expect(cellE3).toStrictEqual([+ [{ f: '=$B3' }],+ ]);+ const cellF3 = getValues(2, 5, 2, 5, 'sheet5');+ expect(cellF3).toStrictEqual([+ [{ f: '=$B3' }],+ ]);+ const cellG3 = getValues(2, 6, 2, 6, 'sheet5');+ expect(cellG3).toStrictEqual([+ [{ f: '=$B3' }],+ ]);+ const cellH3 = getValues(2, 7, 2, 7, 'sheet5');+ expect(cellH3).toStrictEqual([+ [{ f: '=$B3' }],+ ]);+ const cellI3 = getValues(2, 8, 2, 8, 'sheet5');+ expect(cellI3).toStrictEqual([+ [{ f: '=$B3' }],+ ]);++ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();++ const cellB2 = getValues(1, 1, 1, 1, 'sheet5');+ expect(cellB2).toStrictEqual([+ [{ v: 5, t: CellValueType.NUMBER }],+ ]);+ const cellD2 = getValues(1, 3, 1, 3, 'sheet5');+ expect(cellD2).toStrictEqual([+ [{ f: '=$B2' }],+ ]);+ const cellE2 = getValues(1, 4, 1, 4, 'sheet5');+ expect(cellE2).toStrictEqual([+ [{ f: '=$B2', si: 'MFYkBI' }],+ ]);+ const cellF2 = getValues(1, 5, 1, 5, 'sheet5');+ expect(cellF2).toStrictEqual([+ [{ si: 'MFYkBI' }],+ ]);+ const cellG2 = getValues(1, 6, 1, 6, 'sheet5');+ expect(cellG2).toStrictEqual([+ [{ si: 'MFYkBI' }],+ ]);+ const cellH2 = getValues(1, 7, 1, 7, 'sheet5');+ expect(cellH2).toStrictEqual([+ [{ si: 'MFYkBI' }],+ ]);+ const cellI2 = getValues(1, 8, 1, 8, 'sheet5');+ expect(cellI2).toStrictEqual([+ [{ si: 'MFYkBI' }],+ ]);+ });+it('Insert column, update reference', async () => {const params: IInsertColCommandParams = {unitId: 'test',diff --git a/packages/sheets/src/commands/commands/insert-row-col.command.ts b/packages/sheets/src/commands/commands/insert-row-col.command.tsindex 7eba1cbbae..2f1bf9d2fb 100644--- a/packages/sheets/src/commands/commands/insert-row-col.command.ts+++ b/packages/sheets/src/commands/commands/insert-row-col.command.ts@@ -134,7 +134,7 @@ export const InsertRowByRangeCommand: ICommand = {const undos: IMutationInfo[] = [{ id: RemoveRowMutation.id, params: undoRowInsertionParams }];// set range values- if (cellValue) {+ if (cellValue && Object.keys(cellValue).length > 0) {redos.push({id: SetRangeValuesMutation.id,params: {