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 where the formula calculation could not be recalculated after it was interrupted in certain situations (#6454)
wpxp123456a20f30c75011
diff --git a/packages/engine-formula/src/facade/f-formula.ts b/packages/engine-formula/src/facade/f-formula.tsindex c5ec4254e5..6db719687a 100644--- a/packages/engine-formula/src/facade/f-formula.ts+++ b/packages/engine-formula/src/facade/f-formula.ts@@ -166,6 +166,7 @@ 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@@ -192,7 +193,7 @@ export class FFormula extends FBase {}/**- * @deprecated Use `whenComputingCompleteAsync` instead.+ * Waits for the formula calculation to complete.* @returns {Promise<void>} This method returns a promise that resolves when the calculation is complete.*/onCalculationEnd(): Promise<void> {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.tsindex d60cdab25f..a0d65d02a5 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', t: 2, v: 1 }],- [{ f: '=A2', t: 2, v: 2 }],- [{ f: '=A3', t: 2, v: 3 }],- [{ f: '=A4', t: 2, v: 4 }],+ [{ f: '=A1' }],+ [{ f: '=A2' }],+ [{ f: '=A3' }],+ [{ f: '=A4' }],]);expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();const valuesUndo = getValues(0, 1, 5, 1, sheetId);expect(valuesUndo).toStrictEqual([- [{ 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 }],+ [{ f: '=A1' }],+ [{ f: '=A2', si: 'W8Hdfc' }],+ [{ si: 'W8Hdfc' }],+ [{ si: 'W8Hdfc' }],[null],[null],]);@@ -661,10 +661,10 @@ describe('Test update formula ', () => {expect(valuesRedo).toStrictEqual([[null],[null],- [{ f: '=A1', t: 2, v: 1 }],- [{ f: '=A2', t: 2, v: 2 }],- [{ f: '=A3', t: 2, v: 3 }],- [{ f: '=A4', t: 2, v: 4 }],+ [{ f: '=A1' }],+ [{ f: '=A2' }],+ [{ f: '=A3' }],+ [{ f: '=A4' }],]);});@@ -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', t: 2, v: 2 }]]);+ expect(values1).toStrictEqual([[{ f: '=A2' }]]);const values2 = getValues(1, 1, 3, 1, sheetId);expect(values2).toStrictEqual([[null],- [{ f: '=A3', t: 2, v: 3 }],- [{ f: '=A4', t: 2, v: 4 }],+ [{ f: '=A3' }],+ [{ f: '=A4' }],]);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', t: 2, v: 2 }],- [{ si: 'W8Hdfc', t: 2, v: 3 }],- [{ si: 'W8Hdfc', t: 2, v: 4 }],+ [{ f: '=A2', si: 'W8Hdfc' }],+ [{ si: 'W8Hdfc' }],+ [{ si: 'W8Hdfc' }],]);expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();const valuesRedo1 = getValues(1, 3, 1, 3, sheetId);- expect(valuesRedo1).toStrictEqual([[{ f: '=A2', t: 2, v: 2 }]]);+ expect(valuesRedo1).toStrictEqual([[{ f: '=A2' }]]);const valuesRedo2 = getValues(1, 1, 3, 1, sheetId);expect(valuesRedo2).toStrictEqual([[null],- [{ f: '=A3', t: 2, v: 3 }],- [{ f: '=A4', t: 2, v: 4 }],+ [{ f: '=A3' }],+ [{ f: '=A4' }],]);});@@ -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', t: 2, v: 3 }]]);+ expect(values1).toStrictEqual([[{ f: '=A3' }]]);const values2 = getValues(1, 1, 3, 1, sheetId);expect(values2).toStrictEqual([- [{ f: '=A2', si: 'W8Hdfc', t: 2, v: 2 }],+ [{ f: '=A2', si: 'W8Hdfc' }],[null],- [{ si: 'W8Hdfc', t: 2, v: 4 }],+ [{ si: 'W8Hdfc' }],]);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', t: 2, v: 2 }],- [{ si: 'W8Hdfc', t: 2, v: 3 }],- [{ si: 'W8Hdfc', t: 2, v: 4 }],+ [{ f: '=A2', si: 'W8Hdfc' }],+ [{ si: 'W8Hdfc' }],+ [{ si: 'W8Hdfc' }],]);expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();const valuesRedo1 = getValues(2, 3, 2, 3, sheetId);- expect(valuesRedo1).toStrictEqual([[{ f: '=A3', t: 2, v: 3 }]]);+ expect(valuesRedo1).toStrictEqual([[{ f: '=A3' }]]);const valuesRedo2 = getValues(1, 1, 3, 1, sheetId);expect(valuesRedo2).toStrictEqual([- [{ f: '=A2', si: 'W8Hdfc', t: 2, v: 2 }],+ [{ f: '=A2', si: 'W8Hdfc' }],[null],- [{ si: 'W8Hdfc', t: 2, v: 4 }],+ [{ si: 'W8Hdfc' }],]);});@@ -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', t: 2, v: 2 }], [{ f: '=A3', t: 2, v: 3 }]]);+ expect(values1).toStrictEqual([[{ f: '=A2' }], [{ f: '=A3' }]]);const values2 = getValues(1, 1, 3, 1, sheetId);expect(values2).toStrictEqual([[null],[null],- [{ f: '=A4', t: 2, v: 4 }],+ [{ f: '=A4' }],]);expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();const valuesUndo = getValues(1, 1, 3, 1, sheetId);expect(valuesUndo).toStrictEqual([- [{ f: '=A2', si: 'W8Hdfc', t: 2, v: 2 }],- [{ si: 'W8Hdfc', t: 2, v: 3 }],- [{ si: 'W8Hdfc', t: 2, v: 4 }],+ [{ f: '=A2', si: 'W8Hdfc' }],+ [{ si: 'W8Hdfc' }],+ [{ si: 'W8Hdfc' }],]);expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();const valuesRedo1 = getValues(1, 3, 2, 3, sheetId);- expect(valuesRedo1).toStrictEqual([[{ f: '=A2', t: 2, v: 2 }], [{ f: '=A3', t: 2, v: 3 }]]);+ expect(valuesRedo1).toStrictEqual([[{ f: '=A2' }], [{ f: '=A3' }]]);const valuesRedo2 = getValues(1, 1, 3, 1, sheetId);expect(valuesRedo2).toStrictEqual([[null],[null],- [{ f: '=A4', t: 2, v: 4 }],+ [{ f: '=A4' }],]);});@@ -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)', t: 2, v: 1 }],- [null, { f: '=SUM(A20)', t: 2, v: 2 }],- [null, { f: '=SUM(A21)', t: 2, v: 3 }],+ [null, { f: '=SUM(A19)' }],+ [null, { f: '=SUM(A20)' }],+ [null, { f: '=SUM(A21)' }],]);expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();const valuesUndo = getValues(18, 1, 20, 2);expect(valuesUndo).toStrictEqual([- [{ 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],+ [{ f: '=SUM(A19)' }, null],+ [{ f: '=SUM(A20)', si: 'id1' }, null],+ [{ si: 'id1' }, null],]);expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();const valuesRedo = getValues(18, 1, 20, 2);expect(valuesRedo).toStrictEqual([- [null, { f: '=SUM(A19)', t: 2, v: 1 }],- [null, { f: '=SUM(A20)', t: 2, v: 2 }],- [null, { f: '=SUM(A21)', t: 2, v: 3 }],+ [null, { f: '=SUM(A19)' }],+ [null, { f: '=SUM(A20)' }],+ [null, { f: '=SUM(A21)' }],]);});@@ -1586,52 +1586,21 @@ 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)',- 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(values).toStrictEqual([+ [{ f: '=OFFSET(#REF!,1,1)' }, { f: '=OFFSET(#REF!,1,1)' }, { f: '=OFFSET(A1,1,1)' }, 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)',- v: 0,- t: 2,- }, {- f: '=OFFSET(B1,1,1)',- si: 'id2',- v: 1,- t: 2,- }, {- si: 'id2',- v: 1,- t: 2,- }]]);+ expect(valuesUndo).toStrictEqual([+ [null, null, { f: '=OFFSET(A1,1,1)' }, { f: '=OFFSET(B1,1,1)', si: 'id2' }, { si: 'id2' }],+ ]);expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();const valuesRedo = getValues(21, 0, 21, 4);- 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]]);+ expect(valuesRedo).toStrictEqual([+ [{ f: '=OFFSET(#REF!,1,1)' }, { f: '=OFFSET(#REF!,1,1)' }, { f: '=OFFSET(A1,1,1)' }, 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.tsindex 3439bbef2a..87ab393bb0 100644--- a/packages/sheets/src/commands/mutations/set-range-values.mutation.ts+++ b/packages/sheets/src/commands/mutations/set-range-values.mutation.ts@@ -143,6 +143,12 @@ export const SetRangeValuesMutation: IMutation<ISetRangeValuesMutationParams, boconst overwriteCellPropertiesSet = new Set(['f', 'p', 'si', 'custom', 'ref', 'xf']);function mergeCellData(newValue: ICellData, oldValue: ICellData, styles: Styles) {+ // If there is a formula in the new value, delete the old value v key to avoid conflicts+ if (newValue.f || newValue.si) {+ delete oldValue.v;+ delete oldValue.t;+ }+const type = getCellType(styles, newValue, oldValue);Object.keys(newValue).forEach((key) => {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.tsindex 6cfd42f6da..9386ce16c7 100644--- a/tests/formula-integration/src/__testing__/test-formula-move.ts+++ b/tests/formula-integration/src/__testing__/test-formula-move.ts@@ -70,6 +70,9 @@ 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)) {@@ -84,6 +87,8 @@ export async function expectMoveFormulaRowsResultMatchesSnapshot() {// perform undo operationawait testBed.api.undo();+ await testBed.api.getFormula().onCalculationResultApplied();+ await new Promise((resolve) => setTimeout(resolve, 500));// compare the result with the snapshotconst resultSnapshot_undo = workbook.save();@@ -120,6 +125,9 @@ 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)) {@@ -134,6 +142,8 @@ export async function expectMoveFormulaSiRowsResultMatchesSnapshot() {// perform undo operationawait testBed.api.undo();+ await testBed.api.getFormula().onCalculationResultApplied();+ await new Promise((resolve) => setTimeout(resolve, 500));// compare the result with the snapshotconst resultSnapshot_undo = workbook.save();@@ -174,6 +184,9 @@ 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)) {@@ -188,6 +201,8 @@ export async function expectMoveFormulaCellResultMatchesSnapshot() {// perform undo operationawait testBed.api.undo();+ await testBed.api.getFormula().onCalculationResultApplied();+ await new Promise((resolve) => setTimeout(resolve, 500));// compare the result with the snapshotconst 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.tsindex bc2a618fec..261851afaf 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,6 +69,9 @@ export async function expectRemoveRowsOfFilterRowsResultMatchesSnapshot() {// remove rows 2 to 5, where the 3 to 4 rows are filtered rowsworksheet.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)) {@@ -83,6 +86,8 @@ export async function expectRemoveRowsOfFilterRowsResultMatchesSnapshot() {// perform undo operationawait testBed.api.undo();+ await testBed.api.getFormula().onCalculationResultApplied();+ await new Promise((resolve) => setTimeout(resolve, 500));// compare the result with the snapshotconst resultSnapshot_undo = workbook.save();diff --git a/tests/formula-integration/src/__testing__/util.ts b/tests/formula-integration/src/__testing__/util.tsindex 84cb27f940..f69626202a 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);- const formula = testBed.api.getFormula();- await formula.onCalculationEnd();++ await testBed.api.getFormula().onCalculationResultApplied();const resultSnapshot = workbook.save();const snapshotFilePath = path.resolve(snapshotRootDir, `${getTestFilePath()}-result.json`);