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(sheets): fix the issue with selection during autofill (#1707)

ybzkyeb6f8d00bb81
diff --git a/packages/sheets-ui/src/commands/commands/__tests__/auto-fill.controller.spec.ts b/packages/sheets-ui/src/commands/commands/__tests__/auto-fill.controller.spec.ts
index 7353bd56e3..547eae6303 100644
--- a/packages/sheets-ui/src/commands/commands/__tests__/auto-fill.controller.spec.ts
+++ b/packages/sheets-ui/src/commands/commands/__tests__/auto-fill.controller.spec.ts
@@ -26,7 +26,9 @@ import {
} from '@univerjs/core';
import {
AddWorksheetMergeMutation,
+ NORMAL_SELECTION_PLUGIN_NAME,
RemoveWorksheetMergeMutation,
+ SelectionManagerService,
SetRangeValuesMutation,
SetSelectionsOperation,
} from '@univerjs/sheets';
@@ -217,6 +219,14 @@ const TEST_WORKBOOK_DATA = {
},
},
},
+ mergeData: [
+ {
+ startRow: 0,
+ endRow: 2,
+ startColumn: 5,
+ endColumn: 7,
+ },
+ ],
},
},
createdTime: '',
@@ -261,6 +271,7 @@ describe('Test auto fill rules in controller', () => {
endRow: number,
endColumn: number
) => Array<Array<Nullable<IStyleData>>> | undefined;
+ let selectionManagerService: SelectionManagerService;
beforeEach(() => {
const testBed = createCommandTestBed(TEST_WORKBOOK_DATA, [
[ISelectionRenderService, { useClass: SelectionRenderService }],
@@ -280,6 +291,12 @@ describe('Test auto fill rules in controller', () => {
themeService = get(ThemeService);
themeService.setTheme(theme);
autoFillController = get(AutoFillController);
+ selectionManagerService = get(SelectionManagerService);
+ selectionManagerService.setCurrentSelection({
+ pluginName: NORMAL_SELECTION_PLUGIN_NAME,
+ unitId: 'test',
+ sheetId: 'sheet1',
+ });
commandService.registerCommand(SetRangeValuesMutation);
commandService.registerCommand(SetSelectionsOperation);
commandService.registerCommand(RemoveWorksheetMergeMutation);
@@ -513,6 +530,107 @@ describe('Test auto fill rules in controller', () => {
expect(workbook.getSheetBySheetId('sheet1')?.getCell(7, 7)?.v).toBe('第 4');
});
});
+
+ describe('auto fill the merged cell', async () => {
+ it('test primary', async () => {
+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();
+ if (!workbook) throw new Error('This is an error');
+
+ const selectionManagerService = get(SelectionManagerService);
+ selectionManagerService.add([{
+ style: null,
+ range: {
+ startRow: 0,
+ startColumn: 5,
+ endRow: 2,
+ endColumn: 7,
+ },
+ primary: {
+ startRow: 0,
+ startColumn: 5,
+ endRow: 2,
+ endColumn: 7,
+ actualColumn: 5,
+ actualRow: 0,
+ isMerged: true,
+ isMergedMainCell: true,
+ },
+ }]);
+
+ (autoFillController as any)._triggerAutoFill(
+ {
+ startRow: 0,
+ startColumn: 5,
+ endRow: 2,
+ endColumn: 7,
+ },
+ {
+ startRow: 0,
+ startColumn: 5,
+ endRow: 5,
+ endColumn: 7,
+ }
+ );
+ expect(workbook.getActiveSheet().getMergeData().length).toBe(2);
+ expect(selectionManagerService.getLast()?.primary).toEqual(expect.objectContaining({
+ startRow: 0,
+ startColumn: 5,
+ endRow: 2,
+ endColumn: 7,
+ }));
+ });
+ });
+
+ describe('auto fill clear', async () => {
+ it('test primary will move to upper left corner', async () => {
+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();
+ if (!workbook) throw new Error('This is an error');
+
+ const selectionManagerService = get(SelectionManagerService);
+ selectionManagerService.add([{
+ style: null,
+ range: {
+ startRow: 0,
+ startColumn: 0,
+ endRow: 5,
+ endColumn: 2,
+ },
+ // primary not in the upper left corner, but after fill clear will move to upper left corner
+ primary: {
+ startRow: 0,
+ startColumn: 0,
+ endRow: 1,
+ endColumn: 0,
+ actualColumn: 0,
+ actualRow: 1,
+ isMerged: true,
+ isMergedMainCell: true,
+ },
+ }]);
+
+ (autoFillController as any)._triggerAutoFill(
+ {
+ startRow: 0,
+ startColumn: 0,
+ endRow: 5,
+ endColumn: 2,
+ },
+ {
+ startRow: 0,
+ startColumn: 0,
+ endRow: 3,
+ endColumn: 2,
+ }
+ );
+ expect(workbook.getSheetBySheetId('sheet1')?.getCell(4, 0)?.v).toBeFalsy();
+ expect(selectionManagerService.getLast()?.primary).toEqual(expect.objectContaining({
+ startRow: 0,
+ startColumn: 0,
+ endRow: 0,
+ endColumn: 0,
+ }));
+ });
+ });
});
describe('auto fill range is auto detected', async () => {
diff --git a/packages/sheets-ui/src/commands/commands/auto-fill.command.ts b/packages/sheets-ui/src/commands/commands/auto-fill.command.ts
index 8782584de7..0113e51054 100644
--- a/packages/sheets-ui/src/commands/commands/auto-fill.command.ts
+++ b/packages/sheets-ui/src/commands/commands/auto-fill.command.ts
@@ -17,12 +17,12 @@
import type { ICommand, IRange } from '@univerjs/core';
import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService } from '@univerjs/core';
import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';
-import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory, SetSelectionsOperation } from '@univerjs/sheets';
+import { SelectionManagerService, SetRangeValuesMutation, SetRangeValuesUndoMutationFactory, SetSelectionsOperation } from '@univerjs/sheets';
import type { IAccessor } from '@wendellhu/redi';
import { generateNullCellValue } from '../../services/auto-fill/tools';
-export interface IAutoFillCommandParams {}
+export interface IAutoFillCommandParams { }
export const AutoFillCommand: ICommand = {
type: CommandType.COMMAND,
@@ -43,6 +43,7 @@ export const AutoClearContentCommand: ICommand = {
type: CommandType.COMMAND,
handler: async (accessor: IAccessor, params: IAutoClearContentCommand) => {
const univerInstanceService = accessor.get(IUniverInstanceService);
+ const selectionManagerService = accessor.get(SelectionManagerService);
const commandService = accessor.get(ICommandService);
const undoRedoService = accessor.get(IUndoRedoService);
@@ -61,11 +62,19 @@ export const AutoClearContentCommand: ICommand = {
accessor,
clearMutationParams
);
+ const { startColumn, startRow } = selectionRange;
commandService.executeCommand(SetSelectionsOperation.id, {
selections: [
{
primary: {
- ...selectionRange,
+ startColumn,
+ startRow,
+ endColumn: startColumn,
+ endRow: startRow,
+ actualRow: startRow,
+ actualColumn: startColumn,
+ isMerge: false,
+ isMergedMainCell: false,
},
range: {
...selectionRange,
diff --git a/packages/sheets-ui/src/controllers/auto-fill.controller.ts b/packages/sheets-ui/src/controllers/auto-fill.controller.ts
index 05eb055917..6938540771 100644
--- a/packages/sheets-ui/src/controllers/auto-fill.controller.ts
+++ b/packages/sheets-ui/src/controllers/auto-fill.controller.ts
@@ -365,13 +365,7 @@ export class AutoFillController extends Disposable {
this._commandService.syncExecuteCommand(SetSelectionsOperation.id, {
selections: [
{
- primary: {
- ...selection,
- actualRow: selection.startRow,
- actualColumn: selection.startColumn,
- endColumn: selection.startColumn,
- endRow: selection.startRow,
- },
+ primary: { ...(this._selectionManagerService.getLast()?.primary ?? selection) },
range: {
...selection,
rangeType: RANGE_TYPE.NORMAL,