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): auto fill twice (#687)
Dushusir9a8a523d25c1
diff --git a/packages/sheets-formula/src/controllers/__tests__/formula-auto-fill.controller.spec.ts b/packages/sheets-formula/src/controllers/__tests__/formula-auto-fill.controller.spec.tsindex 60eb2e89ef..316e6d3eff 100644--- a/packages/sheets-formula/src/controllers/__tests__/formula-auto-fill.controller.spec.ts+++ b/packages/sheets-formula/src/controllers/__tests__/formula-auto-fill.controller.spec.ts@@ -7,7 +7,6 @@ import {ThemeService,UndoCommand,} from '@univerjs/core';-import { FormulaEngineService } from '@univerjs/engine-formula';import {AddWorksheetMergeMutation,NORMAL_SELECTION_PLUGIN_NAME,@@ -15,6 +14,7 @@ import {SelectionManagerService,SetRangeValuesMutation,SetSelectionsOperation,+ SheetInterceptorService,} from '@univerjs/sheets';import {AutoFillCommand,@@ -24,6 +24,7 @@ import {ISelectionRenderService,SelectionRenderService,} from '@univerjs/sheets-ui';+import { DesktopPlatformService, DesktopShortcutService, IPlatformService, IShortcutService } from '@univerjs/ui';import type { Injector } from '@wendellhu/redi';import { afterEach, beforeEach, describe, expect, it } from 'vitest';@@ -49,10 +50,12 @@ describe('Test auto fill with formula', () => {beforeEach(() => {const testBed = createCommandTestBed(undefined, [+ [SheetInterceptorService],[ISelectionRenderService, { useClass: SelectionRenderService }],[AutoFillController],- [FormulaEngineService],[IAutoFillService, { useClass: AutoFillService }],+ [IShortcutService, { useClass: DesktopShortcutService }],+ [IPlatformService, { useClass: DesktopPlatformService }],[FormulaAutoFillController],]);univer = testBed.univer;@@ -114,7 +117,7 @@ describe('Test auto fill with formula', () => {});- // values will be in the following format+ // B1:B3 values will be in the following format// [// [ { f: '=SUM(A1)' } ],// [ { f: '=SUM(A2)', si: '1ZMZWH' } ],@@ -134,7 +137,10 @@ describe('Test auto fill with formula', () => {B2 = values && values[1][0];B3 = values && values[2][0];- expect(B2).toStrictEqual({});+ expect(B2).toStrictEqual({+ t: 2,+ v: 1,+ });expect(B3).toStrictEqual({});// redo@@ -146,6 +152,117 @@ describe('Test auto fill with formula', () => {expect(B2?.f).toStrictEqual('=SUM(A2)');expect(B2?.si).toEqual(B3?.si);+ // drop to right+ (autoFillController as any)._handleFill(+ {+ startColumn: 1,+ endColumn: 1,+ startRow: 0,+ endRow: 2,+ },+ {+ startColumn: 1,+ endColumn: 3,+ startRow: 0,+ endRow: 2,+ }+ );++ // B1:D3 values will be in the following format+ // [+ // [+ // {+ // "f": "=SUM(A1)",+ // },+ // {+ // "f": "=SUM(B1)",+ // "si": "tF5X2Q"+ // },+ // {+ // "si": "tF5X2Q"+ // }+ // ],+ // [+ // {+ // "f": "=SUM(A2)",+ // "si": "d4_488"+ // },+ // {+ // "f": "=SUM(B2)",+ // "si": "d4_488"+ // },+ // {+ // "si": "d4_488"+ // }+ // ],+ // [+ // {+ // "si": "d4_488"+ // },+ // {+ // "si": "d4_488"+ // },+ // {+ // "si": "d4_488"+ // }+ // ]+ // ]+ values = getValues(0, 1, 2, 3);+ let C1 = values && values[0][1];+ let C2 = values && values[1][1];+ let C3 = values && values[2][1];+ let D1 = values && values[0][2];+ let D2 = values && values[1][2];+ let D3 = values && values[2][2];++ expect(C1?.f).toStrictEqual('=SUM(B1)');+ expect(C2?.f).toStrictEqual('=SUM(B2)');+ expect(C1?.si).toEqual(D1?.si);++ expect(B2?.si).toEqual(C2?.si);+ expect(D2?.si).toEqual(C2?.si);++ expect(B3?.si).toEqual(C3?.si);+ expect(D3?.si).toEqual(C3?.si);++ // undo+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();++ values = getValues(0, 1, 2, 3);+ C1 = values && values[0][1];+ C2 = values && values[1][1];+ C3 = values && values[2][1];+ D1 = values && values[0][2];+ D2 = values && values[1][2];+ D3 = values && values[2][2];++ expect(C1).toStrictEqual({});+ expect(C2).toStrictEqual({});+ expect(C3).toStrictEqual({});+ expect(D1).toStrictEqual({});+ expect(D2).toStrictEqual({});+ expect(D3).toStrictEqual({});++ // redo+ expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();+ values = getValues(0, 1, 2, 3);+ C1 = values && values[0][1];+ C2 = values && values[1][1];+ C3 = values && values[2][1];+ D1 = values && values[0][2];+ D2 = values && values[1][2];+ D3 = values && values[2][2];++ expect(C1?.f).toStrictEqual('=SUM(B1)');+ expect(C2?.f).toStrictEqual('=SUM(B2)');+ expect(C1?.si).toEqual(D1?.si);++ expect(B2?.si).toEqual(C2?.si);+ expect(D2?.si).toEqual(C2?.si);++ expect(B3?.si).toEqual(C3?.si);+ expect(D3?.si).toEqual(C3?.si);+// Restore the original dataexpect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();});diff --git a/packages/sheets-formula/src/controllers/__tests__/formula-clipboard.controller.spec.ts b/packages/sheets-formula/src/controllers/__tests__/formula-clipboard.controller.spec.tsindex c235a0c276..94bca7bb19 100644--- a/packages/sheets-formula/src/controllers/__tests__/formula-clipboard.controller.spec.ts+++ b/packages/sheets-formula/src/controllers/__tests__/formula-clipboard.controller.spec.ts@@ -25,7 +25,6 @@ describe('Test paste with formula', () => {beforeEach(() => {const testBed = createCommandTestBed(undefined, [[ISelectionRenderService, { useClass: SelectionRenderService }],- [FormulaEngineService],]);univer = testBed.univer;get = testBed.get;@@ -103,7 +102,6 @@ describe('Test paste with formula', () => {si: null,p: null,v: null,- m: null,t: null,},'3': {@@ -112,7 +110,6 @@ describe('Test paste with formula', () => {si: null,p: null,v: null,- m: null,t: null,},},diff --git a/packages/sheets-formula/src/controllers/formula-auto-fill.controller.ts b/packages/sheets-formula/src/controllers/formula-auto-fill.controller.tsindex 2e949b9ccd..ea95a7cc47 100644--- a/packages/sheets-formula/src/controllers/formula-auto-fill.controller.ts+++ b/packages/sheets-formula/src/controllers/formula-auto-fill.controller.ts@@ -1,5 +1,13 @@import type { ICellData, Nullable } from '@univerjs/core';-import { Direction, Disposable, isFormulaString, LifecycleStages, OnLifecycle, Tools } from '@univerjs/core';+import {+ Direction,+ Disposable,+ isFormulaId,+ isFormulaString,+ LifecycleStages,+ OnLifecycle,+ Tools,+} from '@univerjs/core';import { FormulaEngineService } from '@univerjs/engine-formula';import type { AutoFillService, IAutoFillRule, ICopyDataInTypeIndexInfo, ICopyDataPiece } from '@univerjs/sheets-ui';import { APPLY_TYPE, DATA_TYPE, IAutoFillService } from '@univerjs/sheets-ui';@@ -24,7 +32,7 @@ export class FormulaAutoFillController extends Disposable {const formulaRule: IAutoFillRule = {type: DATA_TYPE.FORMULA,priority: 1001,- match: (cellData) => isFormulaString(cellData?.f),+ match: (cellData) => isFormulaString(cellData?.f) || isFormulaId(cellData?.si),isContinue: (prev, cur) => {if (prev.type === DATA_TYPE.FORMULA) {return true;@@ -57,14 +65,18 @@ export class FormulaAutoFillController extends Disposable {const d = Tools.deepClone(data[index]);if (d) {- const originalFormula = data[index]?.f;+ const originalFormula = data[index]?.f || '';+ const originalFormulaId = data[index]?.si || '';- if (originalFormula) {+ const checkFormula = isFormulaString(originalFormula);+ const checkFormulaId = isFormulaId(originalFormulaId);++ if (checkFormula) {// The first position setting formula and formulaIdlet formulaId = formulaIdMap.get(index);if (!formulaId) {- formulaId = Tools.generateRandomId(6);+ formulaId = checkFormulaId ? originalFormulaId : Tools.generateRandomId(6);formulaIdMap.set(index, formulaId);const { offsetX, offsetY } = directionToOffset(step, direction);@@ -86,6 +98,17 @@ export class FormulaAutoFillController extends Disposable {d.p = null;}+ if (direction === Direction.DOWN || direction === Direction.RIGHT) {+ applyData.push(d);+ } else {+ applyData.unshift(d);+ }+ } else if (checkFormulaId) {+ d.si = originalFormulaId;+ d.f = null;+ d.v = null;+ d.p = null;+if (direction === Direction.DOWN || direction === Direction.RIGHT) {applyData.push(d);} else {