From fa83993ec7b2b8af9b2a5ac4a8b0e5a8bb610df4 Mon Sep 17 00:00:00 2001 From: wpxp123456 <2677556700@qq.com> Date: Wed, 14 Jan 2026 15:07:53 +0800 Subject: [PATCH] revert: "fix(formula): fix where the formula calculation could not be recalculated after it was interrupted in certain situations (#6454)" (#6464) --- .../engine-formula/src/facade/f-formula.ts | 3 +- .../update-formula.controller.spec.ts | 141 +++++++++++------- .../mutations/set-range-values.mutation.ts | 6 - .../src/__testing__/test-formula-move.ts | 15 -- .../test-remove-rows-of-filter-rows.ts | 5 - .../src/__testing__/util.ts | 4 +- 6 files changed, 89 insertions(+), 85 deletions(-) diff --git a/packages/engine-formula/src/facade/f-formula.ts b/packages/engine-formula/src/facade/f-formula.ts index 6db719687a..c5ec4254e5 100644 --- a/packages/engine-formula/src/facade/f-formula.ts +++ b/packages/engine-formula/src/facade/f-formula.ts @@ -166,7 +166,6 @@ export class FFormula extends FBase { } /** - * @deprecated Use `onCalculationEnd` instead. * Wait for computing in the Univer instance to complete. Please note that this does not only include formula calculation, * but also other computing tasks, e.g. pivot table calculation. * @param {number} [timeout] The maximum time to wait for the computing to complete, in milliseconds. The default @@ -193,7 +192,7 @@ export class FFormula extends FBase { } /** - * Waits for the formula calculation to complete. + * @deprecated Use `whenComputingCompleteAsync` instead. * @returns {Promise} This method returns a promise that resolves when the calculation is complete. */ onCalculationEnd(): Promise { diff --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.ts index a0d65d02a5..d60cdab25f 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 @@ -639,19 +639,19 @@ describe('Test update formula ', () => { expect(values).toStrictEqual([ [null], [null], - [{ f: '=A1' }], - [{ f: '=A2' }], - [{ f: '=A3' }], - [{ f: '=A4' }], + [{ f: '=A1', t: 2, v: 1 }], + [{ f: '=A2', t: 2, v: 2 }], + [{ f: '=A3', t: 2, v: 3 }], + [{ f: '=A4', t: 2, v: 4 }], ]); expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy(); const valuesUndo = getValues(0, 1, 5, 1, sheetId); expect(valuesUndo).toStrictEqual([ - [{ f: '=A1' }], - [{ f: '=A2', si: 'W8Hdfc' }], - [{ si: 'W8Hdfc' }], - [{ si: 'W8Hdfc' }], + [{ f: '=A1', t: 2, v: 1 }], + [{ f: '=A2', si: 'W8Hdfc', t: 2, v: 2 }], + [{ si: 'W8Hdfc', t: 2, v: 3 }], + [{ si: 'W8Hdfc', t: 2, v: 4 }], [null], [null], ]); @@ -661,10 +661,10 @@ describe('Test update formula ', () => { expect(valuesRedo).toStrictEqual([ [null], [null], - [{ f: '=A1' }], - [{ f: '=A2' }], - [{ f: '=A3' }], - [{ f: '=A4' }], + [{ f: '=A1', t: 2, v: 1 }], + [{ f: '=A2', t: 2, v: 2 }], + [{ f: '=A3', t: 2, v: 3 }], + [{ f: '=A4', t: 2, v: 4 }], ]); }); @@ -696,12 +696,12 @@ describe('Test update formula ', () => { expect(await commandService.executeCommand(MoveRangeCommand.id, params)).toBeTruthy(); const values1 = getValues(1, 3, 1, 3, sheetId); - expect(values1).toStrictEqual([[{ f: '=A2' }]]); + expect(values1).toStrictEqual([[{ f: '=A2', t: 2, v: 2 }]]); const values2 = getValues(1, 1, 3, 1, sheetId); expect(values2).toStrictEqual([ [null], - [{ f: '=A3' }], - [{ f: '=A4' }], + [{ f: '=A3', t: 2, v: 3 }], + [{ f: '=A4', t: 2, v: 4 }], ]); expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy(); @@ -709,19 +709,19 @@ describe('Test update formula ', () => { expect(valuesUndo1).toStrictEqual([[null]]); const valuesUndo2 = getValues(1, 1, 3, 1, sheetId); expect(valuesUndo2).toStrictEqual([ - [{ f: '=A2', si: 'W8Hdfc' }], - [{ si: 'W8Hdfc' }], - [{ si: 'W8Hdfc' }], + [{ f: '=A2', si: 'W8Hdfc', t: 2, v: 2 }], + [{ si: 'W8Hdfc', t: 2, v: 3 }], + [{ si: 'W8Hdfc', t: 2, v: 4 }], ]); expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy(); const valuesRedo1 = getValues(1, 3, 1, 3, sheetId); - expect(valuesRedo1).toStrictEqual([[{ f: '=A2' }]]); + expect(valuesRedo1).toStrictEqual([[{ f: '=A2', t: 2, v: 2 }]]); const valuesRedo2 = getValues(1, 1, 3, 1, sheetId); expect(valuesRedo2).toStrictEqual([ [null], - [{ f: '=A3' }], - [{ f: '=A4' }], + [{ f: '=A3', t: 2, v: 3 }], + [{ f: '=A4', t: 2, v: 4 }], ]); }); @@ -753,12 +753,12 @@ describe('Test update formula ', () => { expect(await commandService.executeCommand(MoveRangeCommand.id, params)).toBeTruthy(); const values1 = getValues(2, 3, 2, 3, sheetId); - expect(values1).toStrictEqual([[{ f: '=A3' }]]); + expect(values1).toStrictEqual([[{ f: '=A3', t: 2, v: 3 }]]); const values2 = getValues(1, 1, 3, 1, sheetId); expect(values2).toStrictEqual([ - [{ f: '=A2', si: 'W8Hdfc' }], + [{ f: '=A2', si: 'W8Hdfc', t: 2, v: 2 }], [null], - [{ si: 'W8Hdfc' }], + [{ si: 'W8Hdfc', t: 2, v: 4 }], ]); expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy(); @@ -766,19 +766,19 @@ describe('Test update formula ', () => { expect(valuesUndo1).toStrictEqual([[null]]); const valuesUndo2 = getValues(1, 1, 3, 1, sheetId); expect(valuesUndo2).toStrictEqual([ - [{ f: '=A2', si: 'W8Hdfc' }], - [{ si: 'W8Hdfc' }], - [{ si: 'W8Hdfc' }], + [{ f: '=A2', si: 'W8Hdfc', t: 2, v: 2 }], + [{ si: 'W8Hdfc', t: 2, v: 3 }], + [{ si: 'W8Hdfc', t: 2, v: 4 }], ]); expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy(); const valuesRedo1 = getValues(2, 3, 2, 3, sheetId); - expect(valuesRedo1).toStrictEqual([[{ f: '=A3' }]]); + expect(valuesRedo1).toStrictEqual([[{ f: '=A3', t: 2, v: 3 }]]); const valuesRedo2 = getValues(1, 1, 3, 1, sheetId); expect(valuesRedo2).toStrictEqual([ - [{ f: '=A2', si: 'W8Hdfc' }], + [{ f: '=A2', si: 'W8Hdfc', t: 2, v: 2 }], [null], - [{ si: 'W8Hdfc' }], + [{ si: 'W8Hdfc', t: 2, v: 4 }], ]); }); @@ -810,30 +810,30 @@ describe('Test update formula ', () => { expect(await commandService.executeCommand(MoveRangeCommand.id, params)).toBeTruthy(); const values1 = getValues(1, 3, 2, 3, sheetId); - expect(values1).toStrictEqual([[{ f: '=A2' }], [{ f: '=A3' }]]); + expect(values1).toStrictEqual([[{ f: '=A2', t: 2, v: 2 }], [{ f: '=A3', t: 2, v: 3 }]]); const values2 = getValues(1, 1, 3, 1, sheetId); expect(values2).toStrictEqual([ [null], [null], - [{ f: '=A4' }], + [{ f: '=A4', t: 2, v: 4 }], ]); expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy(); const valuesUndo = getValues(1, 1, 3, 1, sheetId); expect(valuesUndo).toStrictEqual([ - [{ f: '=A2', si: 'W8Hdfc' }], - [{ si: 'W8Hdfc' }], - [{ si: 'W8Hdfc' }], + [{ f: '=A2', si: 'W8Hdfc', t: 2, v: 2 }], + [{ si: 'W8Hdfc', t: 2, v: 3 }], + [{ si: 'W8Hdfc', t: 2, v: 4 }], ]); expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy(); const valuesRedo1 = getValues(1, 3, 2, 3, sheetId); - expect(valuesRedo1).toStrictEqual([[{ f: '=A2' }], [{ f: '=A3' }]]); + expect(valuesRedo1).toStrictEqual([[{ f: '=A2', t: 2, v: 2 }], [{ f: '=A3', t: 2, v: 3 }]]); const valuesRedo2 = getValues(1, 1, 3, 1, sheetId); expect(valuesRedo2).toStrictEqual([ [null], [null], - [{ f: '=A4' }], + [{ f: '=A4', t: 2, v: 4 }], ]); }); @@ -858,25 +858,25 @@ describe('Test update formula ', () => { expect(await commandService.executeCommand(MoveRangeCommand.id, params)).toBeTruthy(); const values = getValues(18, 1, 20, 2); expect(values).toStrictEqual([ - [null, { f: '=SUM(A19)' }], - [null, { f: '=SUM(A20)' }], - [null, { f: '=SUM(A21)' }], + [null, { f: '=SUM(A19)', t: 2, v: 1 }], + [null, { f: '=SUM(A20)', t: 2, v: 2 }], + [null, { f: '=SUM(A21)', t: 2, v: 3 }], ]); expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy(); const valuesUndo = getValues(18, 1, 20, 2); expect(valuesUndo).toStrictEqual([ - [{ f: '=SUM(A19)' }, null], - [{ f: '=SUM(A20)', si: 'id1' }, null], - [{ si: 'id1' }, null], + [{ f: '=SUM(A19)', t: 2, v: 1 }, null], + [{ f: '=SUM(A20)', si: 'id1', t: 2, v: 2 }, null], + [{ si: 'id1', t: 2, v: 3 }, null], ]); expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy(); const valuesRedo = getValues(18, 1, 20, 2); expect(valuesRedo).toStrictEqual([ - [null, { f: '=SUM(A19)' }], - [null, { f: '=SUM(A20)' }], - [null, { f: '=SUM(A21)' }], + [null, { f: '=SUM(A19)', t: 2, v: 1 }], + [null, { f: '=SUM(A20)', t: 2, v: 2 }], + [null, { f: '=SUM(A21)', t: 2, v: 3 }], ]); }); @@ -1586,21 +1586,52 @@ describe('Test update formula ', () => { const values = getValues(21, 0, 21, 4); // Ignore the calculation results and only verify the offset information - expect(values).toStrictEqual([ - [{ f: '=OFFSET(#REF!,1,1)' }, { f: '=OFFSET(#REF!,1,1)' }, { f: '=OFFSET(A1,1,1)' }, null, null], - ]); + expect(values).toStrictEqual([[{ + f: '=OFFSET(#REF!,1,1)', + v: 0, + t: 2, + }, { + f: '=OFFSET(#REF!,1,1)', + v: 1, + t: 2, + }, { + f: '=OFFSET(A1,1,1)', + v: 1, + t: 2, + }, null, null]]); expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy(); const valuesUndo = getValues(21, 0, 21, 4); - expect(valuesUndo).toStrictEqual([ - [null, null, { f: '=OFFSET(A1,1,1)' }, { f: '=OFFSET(B1,1,1)', si: 'id2' }, { si: 'id2' }], - ]); + expect(valuesUndo).toStrictEqual([[null, null, { + f: '=OFFSET(A1,1,1)', + v: 0, + t: 2, + }, { + f: '=OFFSET(B1,1,1)', + si: 'id2', + v: 1, + t: 2, + }, { + si: 'id2', + v: 1, + t: 2, + }]]); expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy(); const valuesRedo = getValues(21, 0, 21, 4); - expect(valuesRedo).toStrictEqual([ - [{ f: '=OFFSET(#REF!,1,1)' }, { f: '=OFFSET(#REF!,1,1)' }, { f: '=OFFSET(A1,1,1)' }, null, null], - ]); + expect(valuesRedo).toStrictEqual([[{ + f: '=OFFSET(#REF!,1,1)', + v: 0, + t: 2, + }, { + f: '=OFFSET(#REF!,1,1)', + v: 1, + t: 2, + }, { + f: '=OFFSET(A1,1,1)', + v: 1, + t: 2, + }, null, null]]); }); it('Remove column, removed column contains formula', async () => { diff --git a/packages/sheets/src/commands/mutations/set-range-values.mutation.ts b/packages/sheets/src/commands/mutations/set-range-values.mutation.ts index 87ab393bb0..3439bbef2a 100644 --- a/packages/sheets/src/commands/mutations/set-range-values.mutation.ts +++ b/packages/sheets/src/commands/mutations/set-range-values.mutation.ts @@ -143,12 +143,6 @@ export const SetRangeValuesMutation: IMutation { const cellPropertyKey = key as keyof ICellData; diff --git a/tests/formula-integration/src/__testing__/test-formula-move.ts b/tests/formula-integration/src/__testing__/test-formula-move.ts index 9386ce16c7..6cfd42f6da 100644 --- a/tests/formula-integration/src/__testing__/test-formula-move.ts +++ b/tests/formula-integration/src/__testing__/test-formula-move.ts @@ -70,9 +70,6 @@ export async function expectMoveFormulaRowsResultMatchesSnapshot() { const rowSpec = worksheet.getRange('3:3'); worksheet.moveRows(rowSpec, 4); - await testBed.api.getFormula().onCalculationResultApplied(); - await new Promise((resolve) => setTimeout(resolve, 500)); - const resultSnapshot = workbook.save(); const snapshotFilePath = path.resolve(snapshotRootDir, `${getTestFilePath()}-result.json`); if (fs.existsSync(snapshotFilePath)) { @@ -87,8 +84,6 @@ export async function expectMoveFormulaRowsResultMatchesSnapshot() { // perform undo operation await testBed.api.undo(); - await testBed.api.getFormula().onCalculationResultApplied(); - await new Promise((resolve) => setTimeout(resolve, 500)); // compare the result with the snapshot const resultSnapshot_undo = workbook.save(); @@ -125,9 +120,6 @@ export async function expectMoveFormulaSiRowsResultMatchesSnapshot() { const rowSpec = worksheet.getRange('3:3'); worksheet.moveRows(rowSpec, 4); - await testBed.api.getFormula().onCalculationResultApplied(); - await new Promise((resolve) => setTimeout(resolve, 500)); - const resultSnapshot = workbook.save(); const snapshotFilePath = path.resolve(snapshotRootDir, `${getTestFilePath()}-result.json`); if (fs.existsSync(snapshotFilePath)) { @@ -142,8 +134,6 @@ export async function expectMoveFormulaSiRowsResultMatchesSnapshot() { // perform undo operation await testBed.api.undo(); - await testBed.api.getFormula().onCalculationResultApplied(); - await new Promise((resolve) => setTimeout(resolve, 500)); // compare the result with the snapshot const resultSnapshot_undo = workbook.save(); @@ -184,9 +174,6 @@ export async function expectMoveFormulaCellResultMatchesSnapshot() { toRange, }); - await testBed.api.getFormula().onCalculationResultApplied(); - await new Promise((resolve) => setTimeout(resolve, 500)); - const resultSnapshot = workbook.save(); const snapshotFilePath = path.resolve(snapshotRootDir, `${getTestFilePath()}-result.json`); if (fs.existsSync(snapshotFilePath)) { @@ -201,8 +188,6 @@ export async function expectMoveFormulaCellResultMatchesSnapshot() { // perform undo operation await testBed.api.undo(); - await testBed.api.getFormula().onCalculationResultApplied(); - await new Promise((resolve) => setTimeout(resolve, 500)); // compare the result with the snapshot const resultSnapshot_undo = workbook.save(); diff --git a/tests/formula-integration/src/__testing__/test-remove-rows-of-filter-rows.ts b/tests/formula-integration/src/__testing__/test-remove-rows-of-filter-rows.ts index 261851afaf..bc2a618fec 100644 --- a/tests/formula-integration/src/__testing__/test-remove-rows-of-filter-rows.ts +++ b/tests/formula-integration/src/__testing__/test-remove-rows-of-filter-rows.ts @@ -69,9 +69,6 @@ export async function expectRemoveRowsOfFilterRowsResultMatchesSnapshot() { // remove rows 2 to 5, where the 3 to 4 rows are filtered rows worksheet.deleteRows(1, 4); - await testBed.api.getFormula().onCalculationResultApplied(); - await new Promise((resolve) => setTimeout(resolve, 500)); - const resultSnapshot = workbook.save(); const snapshotFilePath = path.resolve(snapshotRootDir, `${getTestFilePath()}-result.json`); if (fs.existsSync(snapshotFilePath)) { @@ -86,8 +83,6 @@ export async function expectRemoveRowsOfFilterRowsResultMatchesSnapshot() { // perform undo operation await testBed.api.undo(); - await testBed.api.getFormula().onCalculationResultApplied(); - await new Promise((resolve) => setTimeout(resolve, 500)); // compare the result with the snapshot const resultSnapshot_undo = workbook.save(); diff --git a/tests/formula-integration/src/__testing__/util.ts b/tests/formula-integration/src/__testing__/util.ts index f69626202a..84cb27f940 100644 --- a/tests/formula-integration/src/__testing__/util.ts +++ b/tests/formula-integration/src/__testing__/util.ts @@ -47,8 +47,8 @@ export async function expectCalculationResultMatchesSnapshot() { const testSnapshot = JSON.parse(testSnapshotRaw) as IWorkbookData; const workbook = testBed.api.createWorkbook(testSnapshot); - - await testBed.api.getFormula().onCalculationResultApplied(); + const formula = testBed.api.getFormula(); + await formula.onCalculationEnd(); const resultSnapshot = workbook.save(); const snapshotFilePath = path.resolve(snapshotRootDir, `${getTestFilePath()}-result.json`);