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-ui): keep the picked cell as the anchor of a formula reference (#8071)

wpxp123456cfb78716456d
diff --git a/packages/sheets-ui/src/commands/commands/__tests__/set-selection.command.spec.ts b/packages/sheets-ui/src/commands/commands/__tests__/set-selection.command.spec.ts
index fcc8da199b..9c322bee09 100644
--- a/packages/sheets-ui/src/commands/commands/__tests__/set-selection.command.spec.ts
+++ b/packages/sheets-ui/src/commands/commands/__tests__/set-selection.command.spec.ts
@@ -15,12 +15,6 @@
*/
import type { Injector, IWorkbookData, Univer, Workbook } from '@univerjs/core';
-import type {
- IExpandSelectionCommandParams,
- IMoveSelectionCommandParams,
- IMoveSelectionEnterAndTabCommandParams,
- ISelectAllCommandParams,
-} from '../set-selection.command';
import {
BooleanNumber,
Direction,
@@ -48,6 +42,12 @@ import {
} from '@univerjs/sheets';
import { KeyCode } from '@univerjs/ui';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import type {
+ IExpandSelectionCommandParams,
+ IMoveSelectionCommandParams,
+ IMoveSelectionEnterAndTabCommandParams,
+ ISelectAllCommandParams,
+} from '../set-selection.command';
import { SHEETS_UI_PLUGIN_CONFIG_KEY } from '../../../config/config';
import { SelectAllService } from '../../../services/select-all/select-all.service';
import { ISheetSelectionRenderService } from '../../../services/selection/base-selection-render.service';
@@ -680,6 +680,171 @@ describe('Test commands used for change selections', () => {
expectSelectionToBe(0, 1, 1, 2);
});
+
+ it('should keep extending a formula reference upwards while the ref selection has no primary', async () => {
+ prepareSelectionsTestBed();
+ get(IContextService).setContextValue(REF_SELECTIONS_ENABLED, true);
+ const refSelectionsService = get(IRefSelectionsService);
+
+ // The sheet selection keeps the cell the reference was picked from.
+ selectionManagerService.setSelections([
+ {
+ range: { startRow: 7, startColumn: 1, endRow: 7, endColumn: 1, rangeType: RANGE_TYPE.NORMAL },
+ primary: {
+ startRow: 7,
+ startColumn: 1,
+ endRow: 7,
+ endColumn: 1,
+ actualRow: 7,
+ actualColumn: 1,
+ isMerged: false,
+ isMergedMainCell: false,
+ },
+ style: null,
+ },
+ ]);
+ // The ref selection is rebuilt from the formula text, so it carries no primary cell.
+ refSelectionsService.setSelections([
+ {
+ range: { startRow: 6, startColumn: 1, endRow: 7, endColumn: 1, rangeType: RANGE_TYPE.NORMAL },
+ primary: null,
+ style: null,
+ },
+ ]);
+
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.UP,
+ extra: 'formula-editor',
+ });
+
+ expect(refSelectionsService.getCurrentLastSelection()!.range).toEqual({
+ startRow: 5,
+ startColumn: 1,
+ endRow: 7,
+ endColumn: 1,
+ rangeType: RANGE_TYPE.NORMAL,
+ });
+
+ // The picked cell is kept so the next step can extend from it again.
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.UP,
+ extra: 'formula-editor',
+ });
+
+ expect(refSelectionsService.getCurrentLastSelection()!.range).toEqual({
+ startRow: 4,
+ startColumn: 1,
+ endRow: 7,
+ endColumn: 1,
+ rangeType: RANGE_TYPE.NORMAL,
+ });
+ });
+
+ it('should keep extending a formula reference leftwards while the ref selection has no primary', async () => {
+ prepareSelectionsTestBed();
+ get(IContextService).setContextValue(REF_SELECTIONS_ENABLED, true);
+ const refSelectionsService = get(IRefSelectionsService);
+
+ selectionManagerService.setSelections([
+ {
+ range: { startRow: 11, startColumn: 5, endRow: 11, endColumn: 5, rangeType: RANGE_TYPE.NORMAL },
+ primary: {
+ startRow: 11,
+ startColumn: 5,
+ endRow: 11,
+ endColumn: 5,
+ actualRow: 11,
+ actualColumn: 5,
+ isMerged: false,
+ isMergedMainCell: false,
+ },
+ style: null,
+ },
+ ]);
+ refSelectionsService.setSelections([
+ {
+ range: { startRow: 11, startColumn: 4, endRow: 11, endColumn: 5, rangeType: RANGE_TYPE.NORMAL },
+ primary: null,
+ style: null,
+ },
+ ]);
+
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.LEFT,
+ extra: 'formula-editor',
+ });
+
+ expect(refSelectionsService.getCurrentLastSelection()!.range).toEqual({
+ startRow: 11,
+ startColumn: 3,
+ endRow: 11,
+ endColumn: 5,
+ rangeType: RANGE_TYPE.NORMAL,
+ });
+ expect(refSelectionsService.getCurrentLastSelectionPrimaryCell()).toMatchObject({
+ startRow: 11,
+ startColumn: 5,
+ endRow: 11,
+ endColumn: 5,
+ });
+
+ // The second step must keep growing instead of collapsing back to a single cell.
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.LEFT,
+ extra: 'formula-editor',
+ });
+
+ expect(refSelectionsService.getCurrentLastSelection()!.range).toEqual({
+ startRow: 11,
+ startColumn: 2,
+ endRow: 11,
+ endColumn: 5,
+ rangeType: RANGE_TYPE.NORMAL,
+ });
+ });
+
+ it('should shrink a formula reference when moving back towards the picked cell', async () => {
+ prepareSelectionsTestBed();
+ get(IContextService).setContextValue(REF_SELECTIONS_ENABLED, true);
+ const refSelectionsService = get(IRefSelectionsService);
+
+ selectionManagerService.setSelections([
+ {
+ range: { startRow: 7, startColumn: 1, endRow: 7, endColumn: 1, rangeType: RANGE_TYPE.NORMAL },
+ primary: {
+ startRow: 7,
+ startColumn: 1,
+ endRow: 7,
+ endColumn: 1,
+ actualRow: 7,
+ actualColumn: 1,
+ isMerged: false,
+ isMergedMainCell: false,
+ },
+ style: null,
+ },
+ ]);
+ refSelectionsService.setSelections([
+ {
+ range: { startRow: 4, startColumn: 1, endRow: 10, endColumn: 1, rangeType: RANGE_TYPE.NORMAL },
+ primary: null,
+ style: null,
+ },
+ ]);
+
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.DOWN,
+ extra: 'formula-editor',
+ });
+
+ expect(refSelectionsService.getCurrentLastSelection()!.range).toEqual({
+ startRow: 5,
+ startColumn: 1,
+ endRow: 10,
+ endColumn: 1,
+ rangeType: RANGE_TYPE.NORMAL,
+ });
+ });
});
describe('Guard branches', () => {
diff --git a/packages/sheets-ui/src/commands/commands/set-selection.command.ts b/packages/sheets-ui/src/commands/commands/set-selection.command.ts
index 13ae5df6da..20479fa56c 100644
--- a/packages/sheets-ui/src/commands/commands/set-selection.command.ts
+++ b/packages/sheets-ui/src/commands/commands/set-selection.command.ts
@@ -16,7 +16,6 @@
import type { ICommand, IMutationInfo, IRange } from '@univerjs/core';
import type { ISetSelectionsOperationParams } from '@univerjs/sheets';
-import type { IUniverSheetsUIConfig } from '../../config/config';
import {
CommandType,
Direction,
@@ -37,8 +36,10 @@ import {
SelectionMoveType,
SetSelectionsOperation,
SheetInterceptorService,
+ SheetsSelectionsService,
} from '@univerjs/sheets';
import { KeyCode } from '@univerjs/ui';
+import type { IUniverSheetsUIConfig } from '../../config/config';
import { SHEETS_UI_PLUGIN_CONFIG_KEY } from '../../config/config';
import { SelectAllService } from '../../services/select-all/select-all.service';
import { ISheetSelectionRenderService } from '../../services/selection/base-selection-render.service';
@@ -411,8 +412,12 @@ export const ExpandSelectionCommand: ICommand<IExpandSelectionCommandParams> = {
}
// When in formula editor, try to use the last primary cell in ref selections as the primary cell for calculation.
+ // Ref selections are rebuilt from the formula text after every step, so they usually carry no primary cell.
+ // The sheet selection still holds the cell the reference started from, and that cell is the anchor the
+ // reference should be extended from.
if (!primary) {
- const lastSelectionPrimary = selectionsService.getCurrentLastSelectionPrimaryCell();
+ const lastSelectionPrimary = selectionsService.getCurrentLastSelectionPrimaryCell() ??
+ accessor.get(SheetsSelectionsService).getCurrentLastSelectionPrimaryCell();
if (lastSelectionPrimary && Rectangle.contains(startRange, lastSelectionPrimary)) {
primary = lastSelectionPrimary;
}