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(engine-formula): preserve SORT input range cache (#7895)
wpxp123456d94b65c04374
diff --git a/packages/engine-formula/src/functions/lookup/sort/__tests__/index.spec.ts b/packages/engine-formula/src/functions/lookup/sort/__tests__/index.spec.tsindex add445e677..3fe9ced7db 100644--- a/packages/engine-formula/src/functions/lookup/sort/__tests__/index.spec.ts+++ b/packages/engine-formula/src/functions/lookup/sort/__tests__/index.spec.ts@@ -26,6 +26,27 @@ describe('Test sort function', () => {const testFunction = new Sort(FUNCTION_NAMES_LOOKUP.SORT);describe('Sort', () => {+ it.each([+ { sortOrder: 1, expected: [[1, 'a'], [2, 'b'], [3, 'c'], [3, 'c'], [5, 'e']] },+ { sortOrder: -1, expected: [[5, 'e'], [3, 'c'], [3, 'c'], [2, 'b'], [1, 'a']] },+ ])('preserves the input rows when sorting with order $sortOrder', ({ sortOrder, expected }) => {+ const inputValues = [[3, 'c'], [1, 'a'], [2, 'b'], [3, 'c'], [5, 'e']];+ const array = ArrayValueObject.create({+ calculateValueList: transformToValueObject(inputValues),+ rowCount: inputValues.length,+ columnCount: inputValues[0].length,+ unitId: '',+ sheetId: '',+ row: 0,+ column: 0,+ });++ const result = testFunction.calculate(array, NumberValueObject.create(1), NumberValueObject.create(sortOrder));++ expect(getObjectValue(result)).toStrictEqual(expected);+ expect(getObjectValue(array)).toStrictEqual(inputValues);+ });+it('Value is normal', async () => {const array = ArrayValueObject.create({calculateValueList: transformToValueObject([diff --git a/packages/engine-formula/src/functions/lookup/sort/index.ts b/packages/engine-formula/src/functions/lookup/sort/index.tsindex 573397133c..7233f908a0 100644--- a/packages/engine-formula/src/functions/lookup/sort/index.ts+++ b/packages/engine-formula/src/functions/lookup/sort/index.ts@@ -139,7 +139,7 @@ export class Sort extends BaseFunction {return ErrorValueObject.create(ErrorType.VALUE);}- const arrayValue = array.getArrayValue();+ const arrayValue = array.getArrayValue().slice();arrayValue.sort(this._sort(sortIndexValue - 1, sortOrderValue));return ArrayValueObject.create({diff --git a/tests/formula-integration/src/__tests__/formua-correctness.spec.ts b/tests/formula-integration/src/__tests__/formua-correctness.spec.tsindex 6dc8706cdf..a23f777a47 100644--- a/tests/formula-integration/src/__tests__/formua-correctness.spec.ts+++ b/tests/formula-integration/src/__tests__/formua-correctness.spec.ts@@ -1,8 +1,68 @@-import { describe, it } from 'vitest';+import { describe, expect, it } from 'vitest';+import { createFormulaTestBed } from '../__testing__/univer';import { expectCalculationResultMatchesSnapshot } from '../__testing__/util';describe('Test formula correctness', () => {it('basic sum', async () => {await expectCalculationResultMatchesSnapshot();});++ it('preserves shared range values for aggregates calculated after SORT', async () => {+ const testBed = createFormulaTestBed();++ try {+ const workbook = testBed.api.createWorkbook({+ id: 'sort-shared-range',+ name: 'Sort shared range',+ sheetOrder: ['sheet1'],+ sheets: {+ sheet1: {+ id: 'sheet1',+ name: 'Sheet1',+ rowCount: 20,+ columnCount: 10,+ cellData: {+ 0: {+ 0: { v: 3 },+ 1: { v: 'c' },+ 3: { f: '=SORT(A1:A5)' },+ 4: { f: '=SUMIFS(A1:A5,B1:B5,"c")' },+ 5: { f: '=SUM(SORT(A1:A5))*0+SUMIFS(A1:A5,B1:B5,"c")' },+ },+ 1: {+ 0: { v: 1 },+ 1: { v: 'a' },+ 4: { f: '=SUMPRODUCT((B1:B5="c")*A1:A5)' },+ },+ 2: { 0: { v: 2 }, 1: { v: 'b' } },+ 3: { 0: { v: 3 }, 1: { v: 'c' } },+ 4: { 0: { v: 5 }, 1: { v: 'e' } },+ },+ },+ },+ });+ const formula = testBed.api.getFormula();+ await formula.onCalculationResultApplied();+ const sheet = workbook.getActiveSheet();++ expect(sheet.getRange('D1:D5').getValues()).toStrictEqual([[1], [2], [3], [3], [5]]);+ expect(sheet.getRange('E1:E2').getValues()).toStrictEqual([[6], [6]]);+ expect(sheet.getRange('F1').getRawValue()).toBe(6);++ formula.executeCalculation();+ await formula.onCalculationResultApplied();++ expect(sheet.getRange('E1:E2').getValues()).toStrictEqual([[6], [6]]);+ expect(sheet.getRange('F1').getRawValue()).toBe(6);+ expect(sheet.getRange('A1:B5').getValues()).toStrictEqual([+ [3, 'c'],+ [1, 'a'],+ [2, 'b'],+ [3, 'c'],+ [5, 'e'],+ ]);+ } finally {+ testBed.univer.dispose();+ }+ });});