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.

test(sheet): add worksheet test (#412)

YuHong5580391efe22
diff --git a/packages/base-sheets/src/commands/commands/__tests__/copy-worksheet.command.spec.ts b/packages/base-sheets/src/commands/commands/__tests__/copy-worksheet.command.spec.ts
new file mode 100644
index 0000000000..f98b2257ae
--- /dev/null
+++ b/packages/base-sheets/src/commands/commands/__tests__/copy-worksheet.command.spec.ts
@@ -0,0 +1,74 @@
+// test for copy sheet command
+import { ICommandService, IUniverInstanceService, RedoCommand, UndoCommand, Univer, Worksheet } from '@univerjs/core';
+import { Injector } from '@wendellhu/redi';
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+
+import { InsertSheetMutation } from '../../mutations/insert-sheet.mutation';
+import { RemoveSheetMutation } from '../../mutations/remove-sheet.mutation';
+import { SetWorksheetActivateMutation } from '../../mutations/set-worksheet-activate.mutation';
+import { CopySheetCommand } from '../copy-worksheet.command';
+import { RemoveSheetCommand } from '../remove-sheet.command';
+import { SetWorksheetActivateCommand } from '../set-worksheet-activate.command';
+import { createCommandTestBed } from './create-command-test-bed';
+
+describe('Test copy worksheet commands', () => {
+ let univer: Univer;
+ let get: Injector['get'];
+ let commandService: ICommandService;
+
+ beforeEach(() => {
+ const testBed = createCommandTestBed();
+ univer = testBed.univer;
+ get = testBed.get;
+
+ commandService = get(ICommandService);
+ commandService.registerCommand(CopySheetCommand);
+ commandService.registerCommand(InsertSheetMutation);
+ commandService.registerCommand(SetWorksheetActivateMutation);
+ commandService.registerCommand(SetWorksheetActivateCommand);
+ commandService.registerCommand(RemoveSheetCommand);
+ commandService.registerCommand(RemoveSheetMutation);
+ });
+
+ afterEach(() => {
+ univer.dispose();
+ });
+
+ describe('copy sheet', () => {
+ describe('copy the only sheet', async () => {
+ it('correct situation', async () => {
+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();
+ if (!workbook) throw new Error('This is an error');
+ function getSheetCopyPart(sheet: Worksheet) {
+ const config = sheet.getConfig();
+ const { id, name, status, ...rest } = config;
+ return rest;
+ }
+
+ expect(
+ await commandService.executeCommand(SetWorksheetActivateCommand.id, { worksheetId: 'sheet1' })
+ ).toBeTruthy();
+ expect(
+ await commandService.executeCommand(CopySheetCommand.id, {
+ workbookId: 'test',
+ worksheetId: 'sheet1',
+ })
+ ).toBeTruthy();
+
+ const [oldSheet, newSheet] = workbook.getSheets();
+
+ expect(getSheetCopyPart(newSheet)).toEqual(getSheetCopyPart(oldSheet));
+
+ // undo;
+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+ expect(workbook.getSheetSize()).toBe(1);
+ // redo
+ expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
+ expect(workbook.getSheetSize()).toBe(2);
+ const [oldSheet2, newSheet2] = workbook.getSheets();
+
+ expect(getSheetCopyPart(newSheet2)).toEqual(getSheetCopyPart(oldSheet2));
+ });
+ });
+ });
+});
diff --git a/packages/base-sheets/src/commands/commands/__tests__/remove-sheet.command.spec.ts b/packages/base-sheets/src/commands/commands/__tests__/remove-sheet.command.spec.ts
new file mode 100644
index 0000000000..42c62881bf
--- /dev/null
+++ b/packages/base-sheets/src/commands/commands/__tests__/remove-sheet.command.spec.ts
@@ -0,0 +1,74 @@
+// test for remove sheet command
+import { ICommandService, IUniverInstanceService, RedoCommand, UndoCommand, Univer } from '@univerjs/core';
+import { Injector } from '@wendellhu/redi';
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+
+import { InsertSheetMutation } from '../../mutations/insert-sheet.mutation';
+import { RemoveSheetMutation } from '../../mutations/remove-sheet.mutation';
+import { SetWorksheetActivateMutation } from '../../mutations/set-worksheet-activate.mutation';
+import { InsertSheetCommand } from '../insert-sheet.command';
+import { RemoveSheetCommand } from '../remove-sheet.command';
+import { SetWorksheetActivateCommand } from '../set-worksheet-activate.command';
+import { createCommandTestBed } from './create-command-test-bed';
+
+describe('Test remove worksheet commands', () => {
+ let univer: Univer;
+ let get: Injector['get'];
+ let commandService: ICommandService;
+
+ beforeEach(() => {
+ const testBed = createCommandTestBed();
+ univer = testBed.univer;
+ get = testBed.get;
+
+ commandService = get(ICommandService);
+ commandService.registerCommand(RemoveSheetCommand);
+ commandService.registerCommand(RemoveSheetMutation);
+ commandService.registerCommand(InsertSheetCommand);
+ commandService.registerCommand(InsertSheetMutation);
+ commandService.registerCommand(SetWorksheetActivateCommand);
+ commandService.registerCommand(SetWorksheetActivateMutation);
+ });
+
+ afterEach(() => {
+ univer.dispose();
+ });
+
+ describe('remove sheet', () => {
+ describe('remove the only sheet', async () => {
+ it('will be not working if there is only one sheet', async () => {
+ expect(
+ await commandService.executeCommand(RemoveSheetCommand.id, {
+ workbookId: 'test',
+ sheetId: 'sheet1',
+ })
+ ).toBeFalsy();
+ });
+ it('will be working if there are more than one sheet', async () => {
+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();
+ if (!workbook) throw new Error('This is an error');
+
+ const targetActiveSheet = workbook.getActiveSheet();
+ const targetSheetId = targetActiveSheet?.getSheetId();
+ expect(
+ await commandService.executeCommand(SetWorksheetActivateCommand.id, { worksheetId: targetSheetId })
+ ).toBeTruthy();
+
+ expect(await commandService.executeCommand(InsertSheetCommand.id, {})).toBeTruthy();
+ expect(
+ await commandService.executeCommand(RemoveSheetCommand.id, {
+ workbookId: 'test',
+ sheetId: 'sheet1',
+ })
+ ).toBeTruthy();
+
+ // undo
+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+ expect(workbook.getSheetSize()).toBe(2);
+ // redo
+ expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
+ expect(workbook.getSheetSize()).toBe(1);
+ });
+ });
+ });
+});
diff --git a/packages/base-sheets/src/commands/commands/__tests__/set-frozen.command.spec.ts b/packages/base-sheets/src/commands/commands/__tests__/set-frozen.command.spec.ts
new file mode 100644
index 0000000000..84443d691f
--- /dev/null
+++ b/packages/base-sheets/src/commands/commands/__tests__/set-frozen.command.spec.ts
@@ -0,0 +1,163 @@
+// test for set frozen command
+import {
+ ICommandService,
+ IUniverInstanceService,
+ IWorkbookConfig,
+ LocaleType,
+ RedoCommand,
+ UndoCommand,
+ Univer,
+} from '@univerjs/core';
+import { Injector } from '@wendellhu/redi';
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+
+import { SetFrozenMutation } from '../../mutations/set-frozen.mutation';
+import { SetFrozenCommand } from '../set-frozen.command';
+import { createCommandTestBed } from './create-command-test-bed';
+
+const WORKBOOK_DATA_DEMO: IWorkbookConfig = {
+ id: 'test',
+ appVersion: '3.0.0-alpha',
+ sheets: {
+ sheet1: {
+ id: 'sheet1',
+ cellData: {
+ '0': {
+ '0': {
+ v: 'A1',
+ },
+ '1': {
+ v: 'B1',
+ },
+ '2': {
+ v: 'C1',
+ },
+ },
+ '1': {
+ '0': {
+ v: 'A2',
+ },
+ '1': {
+ v: 'B2',
+ },
+ '3': {
+ v: 'D2',
+ },
+ '6': {
+ v: 'G2',
+ },
+ '9': {
+ v: 'J2',
+ },
+ },
+ '2': {
+ '5': {
+ v: 'F3',
+ },
+ '9': {
+ v: 'J3',
+ },
+ },
+ '4': {
+ '2': {
+ v: 'C5',
+ },
+ },
+ '5': {
+ '3': {
+ v: 'D6',
+ },
+ },
+ },
+ mergeData: [],
+ },
+ },
+ createdTime: '',
+ creator: '',
+ extensions: [],
+ lastModifiedBy: '',
+ locale: LocaleType.EN_US,
+ modifiedTime: '',
+ name: '',
+ sheetOrder: [],
+ styles: {},
+ timeZone: '',
+};
+
+describe('Test set frozen commands', () => {
+ let univer: Univer;
+ let get: Injector['get'];
+ let commandService: ICommandService;
+
+ beforeEach(() => {
+ const testBed = createCommandTestBed(WORKBOOK_DATA_DEMO);
+ univer = testBed.univer;
+ get = testBed.get;
+
+ commandService = get(ICommandService);
+ commandService.registerCommand(SetFrozenCommand);
+ commandService.registerCommand(SetFrozenMutation);
+ });
+
+ afterEach(() => {
+ univer.dispose();
+ });
+
+ describe('set frozen', () => {
+ describe('set frozen', async () => {
+ it('correct situation: ', async () => {
+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();
+ if (!workbook) throw new Error('This is an error');
+
+ const targetActiveSheet = workbook.getActiveSheet();
+ const targetSheetId = targetActiveSheet?.getSheetId();
+ const originFreeze = workbook.getSheetBySheetId(targetSheetId)?.getConfig().freeze;
+ expect(
+ await commandService.executeCommand(SetFrozenCommand.id, {
+ startRow: 1,
+ startColumn: 1,
+ xSplit: 1,
+ ySplit: 1,
+ })
+ ).toBeTruthy();
+
+ expect(workbook.getSheetBySheetId(targetSheetId)?.getConfig().freeze).toEqual({
+ startRow: 1,
+ startColumn: 1,
+ xSplit: 1,
+ ySplit: 1,
+ });
+
+ expect(workbook.getSheetBySheetId(targetSheetId)?.getConfig().freeze).toEqual({
+ startRow: 1,
+ startColumn: 1,
+ xSplit: 1,
+ ySplit: 1,
+ });
+
+ // undo;
+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+ expect(workbook.getSheetBySheetId(targetSheetId)?.getConfig().freeze).toEqual(originFreeze);
+ // redo
+ expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
+ expect(workbook.getSheetBySheetId(targetSheetId)?.getConfig().freeze).toEqual({
+ startRow: 1,
+ startColumn: 1,
+ xSplit: 1,
+ ySplit: 1,
+ });
+ });
+
+ it('wrong situation: ', async () => {
+ expect(
+ await commandService.executeCommand(SetFrozenCommand.id, {
+ startRow: 100,
+ startColumn: 100,
+ xSplit: 1,
+ ySplit: 1,
+ })
+ ).toBeFalsy();
+ });
+ });
+ });
+});
diff --git a/packages/base-sheets/src/commands/commands/__tests__/set-worksheet-hide.command.spec.ts b/packages/base-sheets/src/commands/commands/__tests__/set-worksheet-hide.command.spec.ts
index 9963c28752..01fa733548 100644
--- a/packages/base-sheets/src/commands/commands/__tests__/set-worksheet-hide.command.spec.ts
+++ b/packages/base-sheets/src/commands/commands/__tests__/set-worksheet-hide.command.spec.ts
@@ -9,14 +9,12 @@ import {
import { Injector } from '@wendellhu/redi';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
-import {
- InsertSheetCommand,
- InsertSheetMutation,
- SetWorksheetActivateCommand,
- SetWorksheetActivateMutation,
- SetWorksheetHideCommand,
- SetWorksheetHideMutation,
-} from '../../..';
+import { InsertSheetMutation } from '../../mutations/insert-sheet.mutation';
+import { SetWorksheetActivateMutation } from '../../mutations/set-worksheet-activate.mutation';
+import { SetWorksheetHideMutation } from '../../mutations/set-worksheet-hide.mutation';
+import { InsertSheetCommand } from '../insert-sheet.command';
+import { SetWorksheetActivateCommand } from '../set-worksheet-activate.command';
+import { SetWorksheetHideCommand } from '../set-worksheet-hide.command';
import { createCommandTestBed } from './create-command-test-bed';
describe('Test set worksheet hide commands', () => {
@@ -43,7 +41,7 @@ describe('Test set worksheet hide commands', () => {
});
describe('Set worksheet hide', () => {
- it('will set current acitve worksheet hidden', async () => {
+ it('will set current active worksheet hidden', async () => {
const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();
if (!workbook) throw new Error('This is an error');
diff --git a/packages/base-sheets/src/commands/commands/__tests__/set-worksheet-name.command.spec.ts b/packages/base-sheets/src/commands/commands/__tests__/set-worksheet-name.command.spec.ts
new file mode 100644
index 0000000000..4e5533ed2a
--- /dev/null
+++ b/packages/base-sheets/src/commands/commands/__tests__/set-worksheet-name.command.spec.ts
@@ -0,0 +1,54 @@
+// test for set worksheet name command
+import { ICommandService, IUniverInstanceService, RedoCommand, UndoCommand, Univer } from '@univerjs/core';
+import { Injector } from '@wendellhu/redi';
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+
+import { SetWorksheetNameMutation } from '../../mutations/set-worksheet-name.mutation';
+import { SetWorksheetNameCommand } from '../set-worksheet-name.command';
+import { createCommandTestBed } from './create-command-test-bed';
+
+describe('Test set worksheet name commands', () => {
+ let univer: Univer;
+ let get: Injector['get'];
+ let commandService: ICommandService;
+
+ beforeEach(() => {
+ const testBed = createCommandTestBed();
+ univer = testBed.univer;
+ get = testBed.get;
+
+ commandService = get(ICommandService);
+ commandService.registerCommand(SetWorksheetNameMutation);
+ commandService.registerCommand(SetWorksheetNameCommand);
+ });
+
+ afterEach(() => {
+ univer.dispose();
+ });
+
+ describe('set worksheet name', () => {
+ describe('set worksheet name', async () => {
+ it('correct situation: ', async () => {
+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();
+ if (!workbook) throw new Error('This is an error');
+
+ expect(
+ await commandService.executeCommand(SetWorksheetNameCommand.id, {
+ workbookId: 'test',
+ worksheetId: 'sheet1',
+ name: 'new name',
+ })
+ ).toBeTruthy();
+
+ expect(workbook.getSheetBySheetId('sheet1')?.getConfig().name).toEqual('new name');
+
+ // undo;
+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+ expect(workbook.getSheetBySheetId('sheet1')?.getConfig().name).toEqual('sheet1');
+ // redo
+ expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
+ expect(workbook.getSheetBySheetId('sheet1')?.getConfig().name).toEqual('new name');
+ });
+ });
+ });
+});
diff --git a/packages/base-sheets/src/commands/commands/__tests__/set-worksheet-order.command.spec.ts b/packages/base-sheets/src/commands/commands/__tests__/set-worksheet-order.command.spec.ts
new file mode 100644
index 0000000000..67e20b89c6
--- /dev/null
+++ b/packages/base-sheets/src/commands/commands/__tests__/set-worksheet-order.command.spec.ts
@@ -0,0 +1,73 @@
+// test for set worksheet name command
+import { ICommandService, IUniverInstanceService, RedoCommand, UndoCommand, Univer } from '@univerjs/core';
+import { Injector } from '@wendellhu/redi';
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+
+import { InsertSheetMutation } from '../../mutations/insert-sheet.mutation';
+import { SetWorksheetActivateMutation } from '../../mutations/set-worksheet-activate.mutation';
+import { SetWorksheetOrderMutation } from '../../mutations/set-worksheet-order.mutation';
+import { InsertSheetCommand } from '../insert-sheet.command';
+import { SetWorksheetActivateCommand } from '../set-worksheet-activate.command';
+import { SetWorksheetOrderCommand } from '../set-worksheet-order.command';
+import { createCommandTestBed } from './create-command-test-bed';
+
+describe('Test set worksheet order commands', () => {
+ let univer: Univer;
+ let get: Injector['get'];
+ let commandService: ICommandService;
+
+ beforeEach(() => {
+ const testBed = createCommandTestBed();
+ univer = testBed.univer;
+ get = testBed.get;
+
+ commandService = get(ICommandService);
+ commandService.registerCommand(SetWorksheetOrderCommand);
+ commandService.registerCommand(SetWorksheetOrderMutation);
+ commandService.registerCommand(SetWorksheetActivateCommand);
+ commandService.registerCommand(SetWorksheetActivateMutation);
+ commandService.registerCommand(InsertSheetCommand);
+ commandService.registerCommand(InsertSheetMutation);
+ });
+
+ afterEach(() => {
+ univer.dispose();
+ });
+
+ describe('set worksheet order', () => {
+ describe('set worksheet order', async () => {
+ it('correct situation: ', async () => {
+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();
+ if (!workbook) throw new Error('This is an error');
+
+ const targetActiveSheet = workbook.getActiveSheet();
+ const targetSheetId = targetActiveSheet?.getSheetId();
+
+ expect(
+ await commandService.executeCommand(SetWorksheetActivateCommand.id, { worksheetId: targetSheetId })
+ ).toBeTruthy();
+
+ expect(await commandService.executeCommand(InsertSheetCommand.id, {})).toBeTruthy();
+ expect(await commandService.executeCommand(InsertSheetCommand.id, {})).toBeTruthy();
+ expect(await commandService.executeCommand(InsertSheetCommand.id, {})).toBeTruthy();
+ expect(await commandService.executeCommand(InsertSheetCommand.id, {})).toBeTruthy();
+
+ expect(
+ await commandService.executeCommand(SetWorksheetOrderCommand.id, {
+ order: 2,
+ workbookId: 'test',
+ worksheetId: 'sheet1',
+ })
+ ).toBeTruthy();
+
+ expect(workbook.getConfig().sheetOrder.indexOf('sheet1')).toEqual(2);
+ // undo;
+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+ expect(workbook.getConfig().sheetOrder.indexOf('sheet1')).toEqual(0);
+ // redo
+ expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
+ expect(workbook.getConfig().sheetOrder.indexOf('sheet1')).toEqual(2);
+ });
+ });
+ });
+});
diff --git a/packages/base-sheets/src/commands/commands/__tests__/set-worksheet-show.command.spec.ts b/packages/base-sheets/src/commands/commands/__tests__/set-worksheet-show.command.spec.ts
new file mode 100644
index 0000000000..9f9c670675
--- /dev/null
+++ b/packages/base-sheets/src/commands/commands/__tests__/set-worksheet-show.command.spec.ts
@@ -0,0 +1,68 @@
+// test for remove sheet command
+import { ICommandService, IUniverInstanceService, RedoCommand, UndoCommand, Univer } from '@univerjs/core';
+import { Injector } from '@wendellhu/redi';
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+
+import { InsertSheetMutation } from '../../mutations/insert-sheet.mutation';
+import { SetWorksheetActivateMutation } from '../../mutations/set-worksheet-activate.mutation';
+import { SetWorksheetHideMutation } from '../../mutations/set-worksheet-hide.mutation';
+import { InsertSheetCommand } from '../insert-sheet.command';
+import { SetWorksheetActivateCommand } from '../set-worksheet-activate.command';
+import { SetWorksheetHideCommand } from '../set-worksheet-hide.command';
+import { createCommandTestBed } from './create-command-test-bed';
+
+describe('Test set worksheet show commands', () => {
+ let univer: Univer;
+ let get: Injector['get'];
+ let commandService: ICommandService;
+
+ beforeEach(() => {
+ const testBed = createCommandTestBed();
+ univer = testBed.univer;
+ get = testBed.get;
+
+ commandService = get(ICommandService);
+ commandService.registerCommand(InsertSheetCommand);
+ commandService.registerCommand(InsertSheetMutation);
+ commandService.registerCommand(SetWorksheetActivateMutation);
+ commandService.registerCommand(SetWorksheetActivateCommand);
+ commandService.registerCommand(SetWorksheetHideCommand);
+ commandService.registerCommand(SetWorksheetHideMutation);
+ });
+
+ afterEach(() => {
+ univer.dispose();
+ });
+
+ describe('set sheet show', () => {
+ describe('set sheet show after make it hidden', async () => {
+ it('correct situation: ', async () => {
+ const workbook = get(IUniverInstanceService).getCurrentUniverSheetInstance();
+ if (!workbook) throw new Error('This is an error');
+
+ const targetActiveSheet = workbook.getActiveSheet();
+ const targetSheetId = targetActiveSheet?.getSheetId();
+ expect(
+ await commandService.executeCommand(SetWorksheetActivateCommand.id, { worksheetId: targetSheetId })
+ ).toBeTruthy();
+
+ expect(await commandService.executeCommand(InsertSheetCommand.id, {})).toBeTruthy();
+ expect(await commandService.executeCommand(InsertSheetCommand.id, {})).toBeTruthy();
+ expect(await commandService.executeCommand(InsertSheetCommand.id, {})).toBeTruthy();
+ expect(await commandService.executeCommand(InsertSheetCommand.id, {})).toBeTruthy();
+ expect(
+ await commandService.executeCommand(SetWorksheetHideCommand.id, { worksheetId: targetSheetId })
+ ).toBeTruthy();
+
+ expect(workbook.getSheetBySheetId(targetSheetId)?.getConfig().hidden).toBeTruthy();
+
+ // undo;
+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+ expect(workbook.getSheetBySheetId(targetSheetId)?.getConfig().hidden).toBeFalsy();
+ // redo
+ expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
+ expect(workbook.getSheetBySheetId(targetSheetId)?.getConfig().hidden).toBeTruthy();
+ });
+ });
+ });
+});
diff --git a/packages/base-sheets/src/commands/commands/remove-sheet.command.ts b/packages/base-sheets/src/commands/commands/remove-sheet.command.ts
index b64a179f20..6ab5eef518 100644
--- a/packages/base-sheets/src/commands/commands/remove-sheet.command.ts
+++ b/packages/base-sheets/src/commands/commands/remove-sheet.command.ts
@@ -41,6 +41,7 @@ export const RemoveSheetCommand: ICommand = {
if (!workbook) return false;
const worksheet = workbook.getSheetBySheetId(worksheetId);
if (!worksheet) return false;
+ if (workbook.getSheets().length <= 1) return false;
const index = workbook.getSheetIndex(worksheet);
const activateSheetId = workbook.getConfig().sheetOrder[index + 1];
diff --git a/packages/base-sheets/src/commands/commands/set-frozen.command.ts b/packages/base-sheets/src/commands/commands/set-frozen.command.ts
index c910fa1d0e..10dc4b11d4 100644
--- a/packages/base-sheets/src/commands/commands/set-frozen.command.ts
+++ b/packages/base-sheets/src/commands/commands/set-frozen.command.ts
@@ -34,6 +34,16 @@ export const SetFrozenCommand: ICommand = {
const worksheet = workbook.getSheetBySheetId(worksheetId);
if (!worksheet) return false;
+ const { startColumn, startRow, xSplit, ySplit } = params;
+
+ if (
+ startRow >= worksheet.getRowCount() ||
+ startColumn >= worksheet.getColumnCount() ||
+ xSplit >= worksheet.getColumnCount() ||
+ ySplit >= worksheet.getRowCount()
+ ) {
+ return false;
+ }
const redoMutationParams: ISetFrozenMutationParams = {
workbookId,
worksheetId,
diff --git a/packages/ui-plugin-sheets/src/commands/commands/set-selection-frozen.command.ts b/packages/ui-plugin-sheets/src/commands/commands/set-selection-frozen.command.ts
new file mode 100644
index 0000000000..d98995cd2c
--- /dev/null
+++ b/packages/ui-plugin-sheets/src/commands/commands/set-selection-frozen.command.ts
@@ -0,0 +1,87 @@
+// TODO@wzhudev: some logic here should be moved to the controller
+
+import {
+ ISetFrozenMutationParams,
+ SelectionManagerService,
+ SetFrozenMutation,
+ SetFrozenMutationFactory,
+} from '@univerjs/base-sheets';
+import {
+ CommandType,
+ ICommand,
+ ICommandService,
+ IUndoRedoService,
+ IUniverInstanceService,
+ RANGE_TYPE,
+} from '@univerjs/core';
+import { IAccessor } from '@wendellhu/redi';
+
+import { ScrollManagerService } from '../../services/scroll-manager.service';
+
+export const SetSelectionFrozenCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.set-selection-frozen',
+ handler: async (accessor: IAccessor) => {
+ const univerInstanceService = accessor.get(IUniverInstanceService);
+ const undoRedoService = accessor.get(IUndoRedoService);
+ const workbookId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();
+ const worksheetId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();
+ const commandService = accessor.get(ICommandService);
+ const selectionManagerService = accessor.get(SelectionManagerService);
+ const selections = selectionManagerService.getSelections();
+ if (!selections) {
+ return false;
+ }
+ const currentSelection = selections[selections?.length - 1];
+ const { range } = currentSelection;
+ const scrollManagerService = accessor.get(ScrollManagerService);
+ const { sheetViewStartRow = 0, sheetViewStartColumn = 0 } = scrollManagerService.getCurrentScroll() || {};
+ let startRow;
+ let startColumn;
+ let ySplit;
+ let xSplit;
+ const { startRow: selectRow, startColumn: selectColumn, rangeType } = range;
+ // Frozen to Row
+ if (rangeType === RANGE_TYPE.ROW) {
+ startRow = selectRow;
+ ySplit = selectRow - sheetViewStartRow;
+ startColumn = -1;
+ xSplit = 0;
+ // Frozen to Column
+ } else if (rangeType === RANGE_TYPE.COLUMN) {
+ startRow = -1;
+ ySplit = 0;
+ startColumn = selectColumn;
+ xSplit = selectColumn - sheetViewStartColumn;
+ // Frozen to Range
+ } else if (rangeType === RANGE_TYPE.NORMAL) {
+ startRow = selectRow;
+ ySplit = selectRow - sheetViewStartRow;
+ startColumn = selectColumn;
+ xSplit = selectColumn - sheetViewStartColumn;
+ // Unexpected value
+ } else {
+ return false;
+ }
+ const redoMutationParams: ISetFrozenMutationParams = {
+ workbookId,
+ worksheetId,
+ startRow,
+ startColumn,
+ xSplit,
+ ySplit,
+ };
+ const undoMutationParams: ISetFrozenMutationParams = SetFrozenMutationFactory(accessor, redoMutationParams);
+
+ const result = commandService.syncExecuteCommand(SetFrozenMutation.id, redoMutationParams);
+ if (result) {
+ undoRedoService.pushUndoRedo({
+ unitID: workbookId,
+ undoMutations: [{ id: SetFrozenMutation.id, params: undoMutationParams }],
+ redoMutations: [{ id: SetFrozenMutation.id, params: redoMutationParams }],
+ });
+ return true;
+ }
+ return true;
+ },
+};