Skip to content

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.

fix(formula): formula auto fill up (#4227)

Dushusire7126ae1ffd8
diff --git a/packages/sheets-formula-ui/src/controllers/__tests__/create-command-test-bed.ts b/packages/sheets-formula-ui/src/controllers/__tests__/create-command-test-bed.ts
index fec686f7f2..c15aa6a508 100644
--- a/packages/sheets-formula-ui/src/controllers/__tests__/create-command-test-bed.ts
+++ b/packages/sheets-formula-ui/src/controllers/__tests__/create-command-test-bed.ts
@@ -43,6 +43,21 @@ const TEST_WORKBOOK_DATA_DEMO: IWorkbookData = {
v: 1,
},
},
+ 9: {
+ 0: {
+ f: '=B10',
+ },
+ },
+ 19: {
+ 0: {
+ v: 20,
+ },
+ },
+ 20: {
+ 0: {
+ f: '=B21',
+ },
+ },
},
},
},
diff --git a/packages/sheets-formula-ui/src/controllers/__tests__/formula-auto-fill.controller.spec.ts b/packages/sheets-formula-ui/src/controllers/__tests__/formula-auto-fill.controller.spec.ts
index 16bf6dfc8c..5acd925b73 100644
--- a/packages/sheets-formula-ui/src/controllers/__tests__/formula-auto-fill.controller.spec.ts
+++ b/packages/sheets-formula-ui/src/controllers/__tests__/formula-auto-fill.controller.spec.ts
@@ -290,5 +290,145 @@ describe('Test auto fill with formula', () => {
// Restore the original data
expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
});
+
+ it('fill up with formula', async () => {
+ const selectionManager = get(SheetsSelectionsService);
+
+ selectionManager.addSelections([
+ {
+ range: { startRow: 9, startColumn: 0, endRow: 9, endColumn: 0, rangeType: RANGE_TYPE.NORMAL },
+ primary: null,
+ style: null,
+ },
+ ]);
+
+ (autoFillController as any)._triggerAutoFill(
+ {
+ startColumn: 0,
+ endColumn: 0,
+ startRow: 9,
+ endRow: 9,
+ },
+ {
+ startColumn: 0,
+ endColumn: 0,
+ startRow: 6,
+ endRow: 9,
+ }
+ );
+
+ // B1:B3 values will be in the following format
+ // [
+ // [ { f: '=B7', si: '1ZMZWH' } ],
+ // [ { si: '1ZMZWH' } ]
+ // [ { si: '1ZMZWH' } ]
+ // [ { f: '=B10' } ],
+ // ]
+ let values = getValues(6, 0, 8, 0);
+ let A7 = values && values[0][0];
+ let A8 = values && values[1][0];
+ let A9 = values && values[2][0];
+
+ expect(A7?.f).toStrictEqual('=B7');
+ expect(A7?.si).toEqual(A8?.si);
+ expect(A7?.si).toEqual(A9?.si);
+
+ // undo
+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+
+ values = getValues(6, 0, 8, 0);
+ A7 = values && values[0][0];
+ A8 = values && values[1][0];
+ A9 = values && values[2][0];
+
+ expect(A7).toStrictEqual({});
+ expect(A8).toStrictEqual({});
+ expect(A9).toStrictEqual({});
+
+ // redo
+ expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
+ values = getValues(6, 0, 8, 0);
+ A7 = values && values[0][0];
+ A8 = values && values[1][0];
+ A9 = values && values[2][0];
+
+ expect(A7?.f).toStrictEqual('=B7');
+ expect(A7?.si).toEqual(A8?.si);
+ expect(A7?.si).toEqual(A9?.si);
+ });
+
+ it('fill up with formula and number', async () => {
+ const selectionManager = get(SheetsSelectionsService);
+
+ selectionManager.addSelections([
+ {
+ range: { startRow: 19, startColumn: 0, endRow: 20, endColumn: 0, rangeType: RANGE_TYPE.NORMAL },
+ primary: null,
+ style: null,
+ },
+ ]);
+
+ (autoFillController as any)._triggerAutoFill(
+ {
+ startColumn: 0,
+ endColumn: 0,
+ startRow: 19,
+ endRow: 20,
+ },
+ {
+ startColumn: 0,
+ endColumn: 0,
+ startRow: 15,
+ endRow: 20,
+ }
+ );
+
+ // B1:B3 values will be in the following format
+ // [
+ // [ { v: 18 } ]
+ // [ { f: '=B17', si: '1ZMZWH' } ],
+ // [ { v: 19 } ]
+ // [ { si: '1ZMZWH' } ]
+ // [ { v: 20 } ]
+ // [ { f: '=B21' } ],
+ // ]
+ let values = getValues(15, 0, 18, 0);
+ let A16 = values && values[0][0];
+ let A17 = values && values[1][0];
+ let A18 = values && values[2][0];
+ let A19 = values && values[3][0];
+
+ expect(A16?.v).toStrictEqual(18);
+ expect(A17?.f).toEqual('=B17');
+ expect(A18?.v).toEqual(19);
+ expect(A19?.si).toEqual(A17?.si);
+
+ // undo
+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+
+ values = getValues(15, 0, 18, 0);
+ A16 = values && values[0][0];
+ A17 = values && values[1][0];
+ A18 = values && values[2][0];
+ A19 = values && values[3][0];
+
+ expect(A16).toStrictEqual({});
+ expect(A17).toStrictEqual({});
+ expect(A18).toStrictEqual({});
+ expect(A19).toStrictEqual({});
+
+ // redo
+ expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
+ values = getValues(15, 0, 18, 0);
+ A16 = values && values[0][0];
+ A17 = values && values[1][0];
+ A18 = values && values[2][0];
+ A19 = values && values[3][0];
+
+ expect(A16?.v).toStrictEqual(18);
+ expect(A17?.f).toEqual('=B17');
+ expect(A18?.v).toEqual(19);
+ expect(A19?.si).toEqual(A17?.si);
+ });
});
});
diff --git a/packages/sheets-formula-ui/src/controllers/formula-auto-fill.controller.ts b/packages/sheets-formula-ui/src/controllers/formula-auto-fill.controller.ts
index 7add902471..3284051d19 100644
--- a/packages/sheets-formula-ui/src/controllers/formula-auto-fill.controller.ts
+++ b/packages/sheets-formula-ui/src/controllers/formula-auto-fill.controller.ts
@@ -87,11 +87,7 @@ export class FormulaAutoFillController extends Disposable {
d.p = null;
d.t = null;
- if (direction === Direction.DOWN || direction === Direction.RIGHT) {
- applyData.push(d);
- } else {
- applyData.unshift(d);
- }
+ applyData.push(d);
} else if (checkFormula) {
// The first position setting formula and formulaId
let formulaId = formulaIdMap.get(index);
@@ -100,7 +96,7 @@ export class FormulaAutoFillController extends Disposable {
formulaId = Tools.generateRandomId(6);
formulaIdMap.set(index, formulaId);
- const { offsetX, offsetY } = directionToOffset(step, direction);
+ const { offsetX, offsetY } = directionToOffset(step, len, direction);
const shiftedFormula = this._lexerTreeBuilder.moveFormulaRefOffset(
originalFormula,
offsetX,
@@ -121,11 +117,7 @@ export class FormulaAutoFillController extends Disposable {
d.t = null;
}
- if (direction === Direction.DOWN || direction === Direction.RIGHT) {
- applyData.push(d);
- } else {
- applyData.unshift(d);
- }
+ applyData.push(d);
}
}
}
@@ -134,13 +126,13 @@ export class FormulaAutoFillController extends Disposable {
}
}
-function directionToOffset(step: number, direction: Direction) {
+function directionToOffset(step: number, len: number, direction: Direction) {
let offsetX = 0;
let offsetY = 0;
switch (direction) {
case Direction.UP:
- offsetY = -step;
+ offsetY = -step * len; // The formula fills upwards, and the cell containing f and si must be at the top.
break;
case Direction.RIGHT:
offsetX = step;
@@ -149,7 +141,7 @@ function directionToOffset(step: number, direction: Direction) {
offsetY = step;
break;
case Direction.LEFT:
- offsetX = -step;
+ offsetX = -step * len; // The formula fills leftwards, and the cell containing f and si must be at the left.
break;
}