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.

chore: move formula cut test to sheets-formula-ui package (#6718)

wpxp123456ecfa2401f908
diff --git a/packages/sheets-formula-ui/src/controllers/__tests__/formula-clipboard.controller.spec.ts b/packages/sheets-formula-ui/src/controllers/__tests__/formula-clipboard.controller.spec.ts
index c6eaab56c8..dbe6abac76 100644
--- a/packages/sheets-formula-ui/src/controllers/__tests__/formula-clipboard.controller.spec.ts
+++ b/packages/sheets-formula-ui/src/controllers/__tests__/formula-clipboard.controller.spec.ts
@@ -14,17 +14,468 @@
* limitations under the License.
*/
-import type { ICellData, Injector, Nullable, Univer } from '@univerjs/core';
+import type { Dependency, ICellData, IDisposable, IRange, IWorkbookData, Nullable, Workbook } from '@univerjs/core';
import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';
import type { ICellDataWithSpanInfo } from '@univerjs/sheets-ui';
-import { ICommandService, IUniverInstanceService, ObjectMatrix } from '@univerjs/core';
-import { FormulaDataModel, LexerTreeBuilder } from '@univerjs/engine-formula';
-import { SetRangeValuesMutation } from '@univerjs/sheets';
-import { COPY_TYPE, ISheetSelectionRenderService, PREDEFINED_HOOK_NAME_PASTE, SheetSelectionRenderService } from '@univerjs/sheets-ui';
+import { DisposableCollection, ICommandService, ILogService, Inject, Injector, IUniverInstanceService, LocaleService, LocaleType, LogLevel, ObjectMatrix, Plugin, RANGE_TYPE, UndoCommand, Univer, UniverInstanceType } from '@univerjs/core';
+import { CalculateFormulaService, DefinedNamesService, FormulaCurrentConfigService, FormulaDataModel, FormulaRuntimeService, HyperlinkEngineFormulaService, ICalculateFormulaService, IDefinedNamesService, IFormulaCurrentConfigService, IFormulaRuntimeService, IHyperlinkEngineFormulaService, LexerTreeBuilder, SetArrayFormulaDataMutation, SetFormulaDataMutation } from '@univerjs/engine-formula';
+import { IRenderManagerService, RenderManagerService } from '@univerjs/engine-render';
+import { discreteRangeToRange, MoveRangeMutation, SetRangeValuesMutation, SetSelectionsOperation, SetWorksheetRowAutoHeightMutation, SheetInterceptorService, SheetSkeletonService, SheetsSelectionsService } from '@univerjs/sheets';
+import { UpdateFormulaController } from '@univerjs/sheets-formula';
+import { COPY_TYPE, IMarkSelectionService, ISheetClipboardService, ISheetSelectionRenderService, PREDEFINED_HOOK_NAME_PASTE, SheetClipboardController, SheetClipboardService, SheetSelectionRenderService, SheetSkeletonManagerService } from '@univerjs/sheets-ui';
+import { BrowserClipboardService, DesktopMessageService, IClipboardInterfaceService, IMessageService, INotificationService, IPlatformService, IUIPartsService, UIPartsService } from '@univerjs/ui';
+import { BehaviorSubject } from 'rxjs';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { getSetCellFormulaMutations } from '../formula-clipboard.controller';
import { createCommandTestBed } from './create-command-test-bed';
+interface ITestSheetClipboardService extends ISheetClipboardService {
+ _generateCopyContent: (
+ unitId: string,
+ subUnitId: string,
+ range: IRange,
+ hooks: unknown[]
+ ) => {
+ matrixFragment: Nullable<ObjectMatrix<ICellDataWithSpanInfo>>;
+ copyId: string;
+ };
+ _pasteInternal: (copyId: string, pasteType: string) => Promise<boolean>;
+}
+
+class testMarkSelectionService {
+ addShape(): string | null {
+ return null;
+ }
+
+ addShapeWithNoFresh(): string | null {
+ return null;
+ }
+
+ removeShape(id: string): void {
+ // empty
+ }
+
+ removeAllShapes(): void {
+ // empty
+ }
+
+ refreshShapes(): void {
+ // empty
+ }
+
+ getShapeMap(): Map<string, any> {
+ return new Map();
+ }
+}
+
+class testNotificationService {
+ show(): IDisposable {
+ return {
+ dispose: () => { /* empty */ },
+ };
+ }
+}
+
+class testPlatformService {
+ isWindows: boolean = false;
+ isMac: boolean = true;
+ isLinux: boolean = false;
+}
+
+export function clipboardTestBed(workbookData?: IWorkbookData, dependencies?: Dependency[]) {
+ const univer = new Univer();
+ const injector = univer.__getInjector();
+ const get = injector.get.bind(injector);
+
+ /**
+ * This plugin hooks into Sheet's DI system to expose API to test scripts
+ */
+ class TestPlugin extends Plugin {
+ static override pluginName = 'test-plugin';
+ static override type = UniverInstanceType.UNIVER_SHEET;
+
+ constructor(
+ _config: undefined,
+ @Inject(Injector) override readonly _injector: Injector
+ ) {
+ super();
+ }
+
+ override onStarting(): void {
+ const injector = this._injector;
+ injector.add([IUIPartsService, { useClass: UIPartsService }]);
+ injector.add([SheetsSelectionsService]);
+ injector.add([IClipboardInterfaceService, { useClass: BrowserClipboardService, lazy: true }]);
+ injector.add([ISheetClipboardService, { useClass: SheetClipboardService }]);
+ injector.add([IMessageService, { useClass: DesktopMessageService, lazy: true }]);
+ injector.add([IMarkSelectionService, { useClass: testMarkSelectionService }]);
+ injector.add([IRenderManagerService, { useClass: RenderManagerService }]);
+ injector.add([ISheetSelectionRenderService, { useClass: SheetSelectionRenderService }]);
+ injector.add([INotificationService, { useClass: testNotificationService }]);
+ injector.add([IPlatformService, { useClass: testPlatformService }]);
+
+ // Because SheetClipboardController is initialized in the rendered life cycle, here we need to initialize it manually
+ const sheetClipboardController = injector.createInstance(SheetClipboardController);
+
+ injector.add([SheetClipboardController, { useValue: sheetClipboardController }]);
+ injector.add([SheetInterceptorService]);
+ injector.add([ICalculateFormulaService, { useClass: CalculateFormulaService }]);
+ injector.add([FormulaDataModel]);
+ injector.add([LexerTreeBuilder]);
+ injector.add([IDefinedNamesService, { useClass: DefinedNamesService }]);
+ injector.add([IHyperlinkEngineFormulaService, { useClass: HyperlinkEngineFormulaService }]);
+ injector.add([IFormulaRuntimeService, { useClass: FormulaRuntimeService }]);
+ injector.add([IFormulaCurrentConfigService, { useClass: FormulaCurrentConfigService }]);
+ injector.add([SheetSkeletonService]);
+
+ dependencies?.forEach((d) => injector.add(d));
+
+ const localeService = injector.get(LocaleService);
+ localeService.load({});
+
+ injector.get(IUIPartsService);
+ }
+ }
+
+ univer.registerPlugin(TestPlugin);
+ const sheet = univer.createUnit<IWorkbookData, Workbook>(UniverInstanceType.UNIVER_SHEET, workbookData || {});
+
+ const univerInstanceService = get(IUniverInstanceService);
+ univerInstanceService.focusUnit('test');
+
+ const logService = get(ILogService);
+ logService.setLogLevel(LogLevel.SILENT); // change this to `LogLevel.VERBOSE` to debug tests via logs
+
+ // NOTE: This is pretty hack for the test. But with these hacks we can avoid to create
+ // real canvas-environment in univerjs/sheets-ui. If some we have to do that, this hack could be removed.
+ const fakeSheetSkeletonManagerService = new SheetSkeletonManagerService({
+ unit: sheet,
+ unitId: 'test',
+ type: UniverInstanceType.UNIVER_SHEET,
+ engine: null as any,
+ scene: null as any,
+ mainComponent: null as any,
+ components: null as any,
+ isMainScene: true,
+ activated$: new BehaviorSubject(true),
+ activate: () => {},
+ deactivate: () => {},
+ }, injector, injector.get(SheetSkeletonService));
+
+ injector.add([SheetSkeletonManagerService, { useValue: fakeSheetSkeletonManagerService }]);
+ injector.get(IRenderManagerService).addRender('test', {
+ type: UniverInstanceType.UNIVER_SHEET,
+ unitId: 'test',
+ engine: new DisposableCollection() as any,
+ scene: new DisposableCollection() as any,
+ mainComponent: null as any,
+ components: new Map(),
+ isMainScene: true,
+ with: injector.get.bind(injector),
+ activated$: new BehaviorSubject(true),
+ activate: () => {},
+ deactivate: () => {},
+ });
+
+ return {
+ univer,
+ get,
+ sheet,
+ };
+}
+
+function createFormulaClipboardWorkbookData(): IWorkbookData {
+ return {
+ id: 'test',
+ appVersion: '3.0.0-alpha',
+ locale: LocaleType.EN_US,
+ name: '',
+ sheetOrder: ['sheet1', 'sheet2'],
+ styles: {},
+ sheets: {
+ sheet1: {
+ id: 'sheet1',
+ name: 'Sheet1',
+ cellData: {
+ 0: {
+ 0: { v: 1 },
+ 1: { v: 2 },
+ 2: { f: '=A1' },
+ 3: { f: '=SUM(A1:B2)' },
+ 4: { f: '=$A1' },
+ 5: { f: '=A$1' },
+ 6: { f: '=$A$1' },
+ 7: { f: '=B1' },
+ 8: { f: '=H1' },
+ 9: { f: '=F3' },
+ 10: { f: '=A1', si: 'shared-ref' },
+ },
+ 1: {
+ 0: { v: 3 },
+ 1: { v: 4 },
+ 10: { si: 'shared-ref' },
+ },
+ 2: {
+ 0: { v: 'label' },
+ 1: { v: 7 },
+ 2: { f: '=B3' },
+ 3: { f: '=C3+1' },
+ 4: { v: 11 },
+ 5: { f: '=SUM(B3:E3)' },
+ },
+ },
+ },
+ sheet2: {
+ id: 'sheet2',
+ name: 'Sheet2',
+ cellData: {
+ 0: {
+ 0: { f: '=Sheet1!A1' },
+ 1: { f: '=Sheet1!H1' },
+ },
+ },
+ },
+ },
+ };
+}
+
+describe('Test cut command with formulas', () => {
+ let univer: Univer;
+ let get: Injector['get'];
+ let commandService: ICommandService;
+ let sheetClipboardService: ISheetClipboardService;
+ let getValues: (
+ startRow: number,
+ startColumn: number,
+ endRow: number,
+ endColumn: number,
+ sheetId?: string
+ ) => Array<Array<Nullable<ICellData>>> | undefined;
+
+ beforeEach(() => {
+ const testBed = clipboardTestBed(createFormulaClipboardWorkbookData(), [
+ [UpdateFormulaController],
+ ]);
+
+ univer = testBed.univer;
+ get = testBed.get;
+ commandService = get(ICommandService);
+ commandService.registerCommand(SetRangeValuesMutation);
+ commandService.registerCommand(SetSelectionsOperation);
+ commandService.registerCommand(MoveRangeMutation);
+ commandService.registerCommand(SetWorksheetRowAutoHeightMutation);
+ commandService.registerCommand(SetFormulaDataMutation);
+ commandService.registerCommand(SetArrayFormulaDataMutation);
+ sheetClipboardService = get(ISheetClipboardService);
+
+ get(UpdateFormulaController);
+
+ getValues = (
+ startRow: number,
+ startColumn: number,
+ endRow: number,
+ endColumn: number,
+ sheetId: string = 'sheet1'
+ ): Array<Array<Nullable<ICellData>>> | undefined =>
+ get(IUniverInstanceService)
+ .getUniverSheetInstance('test')
+ ?.getSheetBySheetId(sheetId)
+ ?.getRange(startRow, startColumn, endRow, endColumn)
+ .getValues();
+ });
+
+ afterEach(() => {
+ univer.dispose();
+ });
+
+ async function cutPaste(
+ fromRange: { rows: number[]; cols: number[] },
+ toRange: { startRow: number; startColumn: number; endRow: number; endColumn: number },
+ fromSubUnitId: string = 'sheet1',
+ toSubUnitId: string = fromSubUnitId
+ ) {
+ const testSheetClipboardService = sheetClipboardService as ITestSheetClipboardService;
+ const copyContentCache = sheetClipboardService.copyContentCache();
+ const workbook = get(IUniverInstanceService).getUniverSheetInstance('test');
+ const targetWorksheet = workbook?.getSheetBySheetId(toSubUnitId);
+
+ if (targetWorksheet) {
+ workbook?.setActiveSheet(targetWorksheet);
+ }
+
+ const { matrixFragment, copyId } = testSheetClipboardService._generateCopyContent(
+ 'test',
+ fromSubUnitId,
+ discreteRangeToRange(fromRange),
+ []
+ );
+
+ copyContentCache.set(copyId, {
+ unitId: 'test',
+ subUnitId: fromSubUnitId,
+ range: fromRange,
+ matrix: matrixFragment,
+ copyType: COPY_TYPE.CUT,
+ });
+
+ get(SheetsSelectionsService).addSelections([
+ {
+ range: { ...toRange, rangeType: RANGE_TYPE.NORMAL },
+ primary: null,
+ style: null,
+ },
+ ]);
+
+ await testSheetClipboardService._pasteInternal(copyId, PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE);
+ }
+
+ it('cut-moving a referenced value range updates direct, range, mixed-absolute, cross-sheet, and shared formulas', async () => {
+ await cutPaste(
+ { rows: [0, 1], cols: [0, 1] },
+ { startRow: 3, startColumn: 3, endRow: 4, endColumn: 4 }
+ );
+
+ expect(getValues(0, 0, 1, 1)).toStrictEqual([
+ [null, null],
+ [null, null],
+ ]);
+ expect(getValues(3, 3, 4, 4)).toStrictEqual([
+ [{ v: 1 }, { v: 2 }],
+ [{ v: 3 }, { v: 4 }],
+ ]);
+
+ expect(getValues(0, 2, 0, 6)).toStrictEqual([
+ [
+ { f: '=D4' },
+ { f: '=SUM(D4:E5)' },
+ { f: '=$D4' },
+ { f: '=D$4' },
+ { f: '=$D$4' },
+ ],
+ ]);
+ expect(getValues(0, 10, 1, 10)).toStrictEqual([
+ [{ f: '=D4' }],
+ [{ f: '=D5' }],
+ ]);
+ expect(getValues(0, 0, 0, 1, 'sheet2')).toStrictEqual([
+ [{ f: '=Sheet1!D4' }, { f: '=Sheet1!H1' }],
+ ]);
+
+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+ expect(getValues(0, 2, 0, 6)).toStrictEqual([
+ [
+ { f: '=A1' },
+ { f: '=SUM(A1:B2)' },
+ { f: '=$A1' },
+ { f: '=A$1' },
+ { f: '=$A$1' },
+ ],
+ ]);
+ expect(getValues(0, 10, 1, 10)).toStrictEqual([
+ [{ f: '=A1', si: 'shared-ref' }],
+ [{ si: 'shared-ref' }],
+ ]);
+ expect(getValues(0, 0, 0, 1, 'sheet2')).toStrictEqual([
+ [{ f: '=Sheet1!A1' }, { f: '=Sheet1!H1' }],
+ ]);
+ });
+
+ it('cut-pasting a referenced value range across sheets adds and removes sheet qualifiers correctly', async () => {
+ await cutPaste(
+ { rows: [0, 1], cols: [0, 1] },
+ { startRow: 2, startColumn: 2, endRow: 3, endColumn: 3 },
+ 'sheet1',
+ 'sheet2'
+ );
+
+ expect(getValues(0, 0, 1, 1)).toStrictEqual([
+ [null, null],
+ [null, null],
+ ]);
+ expect(getValues(2, 2, 3, 3, 'sheet2')).toStrictEqual([
+ [{ v: 1 }, { v: 2 }],
+ [{ v: 3 }, { v: 4 }],
+ ]);
+
+ expect(getValues(0, 2, 0, 6)).toStrictEqual([
+ [
+ { f: '=Sheet2!C3' },
+ { f: '=SUM(Sheet2!C3:D4)' },
+ { f: '=Sheet2!$C3' },
+ { f: '=Sheet2!C$3' },
+ { f: '=Sheet2!$C$3' },
+ ],
+ ]);
+ expect(getValues(0, 0, 0, 1, 'sheet2')).toStrictEqual([
+ [{ f: '=C3' }, { f: '=Sheet1!H1' }],
+ ]);
+ });
+
+ it('cut-moving a formula cell keeps its formula text stable while dependents update to the new address', async () => {
+ await cutPaste(
+ { rows: [0], cols: [7] },
+ { startRow: 3, startColumn: 9, endRow: 3, endColumn: 9 }
+ );
+
+ expect(getValues(0, 7, 0, 8)).toStrictEqual([
+ [null, { f: '=J4' }],
+ ]);
+ expect(getValues(3, 9, 3, 9)).toStrictEqual([
+ [{ f: '=B1' }],
+ ]);
+ expect(getValues(0, 0, 0, 1, 'sheet2')).toStrictEqual([
+ [{ f: '=Sheet1!A1' }, { f: '=Sheet1!J4' }],
+ ]);
+
+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+ expect(getValues(0, 7, 0, 8)).toStrictEqual([
+ [{ f: '=B1' }, { f: '=H1' }],
+ ]);
+ expect(getValues(0, 0, 0, 1, 'sheet2')).toStrictEqual([
+ [{ f: '=Sheet1!A1' }, { f: '=Sheet1!H1' }],
+ ]);
+ });
+
+ it('cut-moving a larger mixed value, text, and formula range preserves moved formulas and updates dependents', async () => {
+ await cutPaste(
+ { rows: [2], cols: [0, 1, 2, 3, 4, 5] },
+ { startRow: 5, startColumn: 0, endRow: 5, endColumn: 5 }
+ );
+
+ expect(getValues(2, 0, 2, 5)).toStrictEqual([
+ [null, null, null, null, null, null],
+ ]);
+ expect(getValues(5, 0, 5, 5)).toStrictEqual([
+ [
+ { v: 'label' },
+ { v: 7 },
+ { f: '=B6' },
+ { f: '=C6+1' },
+ { v: 11 },
+ { f: '=SUM(B6:E6)' },
+ ],
+ ]);
+ expect(getValues(0, 9, 0, 9)).toStrictEqual([
+ [{ f: '=F6' }],
+ ]);
+
+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+ expect(getValues(2, 0, 2, 5)).toStrictEqual([
+ [
+ { v: 'label' },
+ { v: 7 },
+ { f: '=B3' },
+ { f: '=C3+1' },
+ { v: 11 },
+ { f: '=SUM(B3:E3)' },
+ ],
+ ]);
+ expect(getValues(0, 9, 0, 9)).toStrictEqual([
+ [{ f: '=F3' }],
+ ]);
+ });
+});
+
describe('Test paste with formula', () => {
let univer: Univer;
let get: Injector['get'];
diff --git a/packages/sheets-ui/package.json b/packages/sheets-ui/package.json
index 0d34d87178..c929056e58 100644
--- a/packages/sheets-ui/package.json
+++ b/packages/sheets-ui/package.json
@@ -95,7 +95,6 @@
},
"devDependencies": {
"@univerjs-infra/shared": "workspace:*",
- "@univerjs/sheets-formula": "workspace:*",
"@univerjs/themes": "workspace:*",
"postcss": "^8.5.8",
"react": "18.3.1",
diff --git a/packages/sheets-ui/src/index.ts b/packages/sheets-ui/src/index.ts
index 37f9a29454..2d5edfdbdd 100644
--- a/packages/sheets-ui/src/index.ts
+++ b/packages/sheets-ui/src/index.ts
@@ -97,6 +97,7 @@ export { AutoFillUIController } from './controllers/auto-fill-ui.controller';
export { AutoHeightController } from './controllers/auto-height.controller';
export { AutoWidthController } from './controllers/auto-width.controller';
export { CellCustomRenderController } from './controllers/cell-custom-render.controller';
+export { SheetClipboardController } from './controllers/clipboard/clipboard.controller';
export { DragRenderController } from './controllers/drag-render.controller';
export { EditingRenderController } from './controllers/editor/editing.render-controller';
export { FormulaEditorController } from './controllers/editor/formula-editor.controller';
diff --git a/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-service.spec.ts b/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-service.spec.ts
index d3fa8a36ed..625f1bc961 100644
--- a/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-service.spec.ts
+++ b/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-service.spec.ts
@@ -14,11 +14,9 @@
* limitations under the License.
*/
-import type { ICellData, Injector, IRange, IStyleData, IWorkbookData, Nullable, ObjectMatrix, Univer } from '@univerjs/core';
-import type { ICellDataWithSpanInfo } from '../type';
+import type { ICellData, Injector, IRange, IStyleData, Nullable, Univer } from '@univerjs/core';
import type { IClipboardItem } from './mock-clipboard';
-import { ICommandService, IUniverInstanceService, LocaleType, RANGE_TYPE, Rectangle, RedoCommand, UndoCommand } from '@univerjs/core';
-import { SetArrayFormulaDataMutation, SetFormulaDataMutation } from '@univerjs/engine-formula';
+import { ICommandService, IUniverInstanceService, RANGE_TYPE, Rectangle, RedoCommand, UndoCommand } from '@univerjs/core';
import {
AddWorksheetMergeMutation,
discreteRangeToRange,
@@ -31,26 +29,12 @@ import {
SetWorksheetRowHeightMutation,
SheetsSelectionsService,
} from '@univerjs/sheets';
-import { UpdateFormulaController } from '@univerjs/sheets-formula';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { ISheetClipboardService, PREDEFINED_HOOK_NAME_PASTE } from '../clipboard.service';
import { COPY_TYPE } from '../type';
import { clipboardTestBed } from './clipboard-test-bed';
import { MockClipboard } from './mock-clipboard';
-interface ITestSheetClipboardService extends ISheetClipboardService {
- _generateCopyContent: (
- unitId: string,
- subUnitId: string,
- range: IRange,
- hooks: unknown[]
- ) => {
- matrixFragment: Nullable<ObjectMatrix<ICellDataWithSpanInfo>>;
- copyId: string;
- };
- _pasteInternal: (copyId: string, pasteType: string) => Promise<boolean>;
-}
-
describe('Test clipboard', () => {
let univer: Univer;
let get: Injector['get'];
@@ -1131,9 +1115,8 @@ describe('Test clipboard', () => {
const subUnitId = 'sheet1';
const fromRange = { rows: [24], cols: [0] };
const toRange = { startRow: 24, startColumn: 1, endRow: 24, endColumn: 1 };
- const testSheetClipboardService = sheetClipboardService as ITestSheetClipboardService;
const copyContentCache = sheetClipboardService.copyContentCache();
- const { matrixFragment, copyId } = testSheetClipboardService._generateCopyContent(unitId, subUnitId, discreteRangeToRange(fromRange), []);
+ const { matrixFragment, copyId } = (sheetClipboardService as any)._generateCopyContent(unitId, subUnitId, discreteRangeToRange(fromRange), []);
// cache the copy content for internal paste
copyContentCache.set(copyId, {
@@ -1154,7 +1137,7 @@ describe('Test clipboard', () => {
},
]);
- testSheetClipboardService._pasteInternal(copyId, PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE);
+ (sheetClipboardService as any)._pasteInternal(copyId, PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE);
expect(getValues(24, 0, 24, 0)![0][0]).toBe(null);
expect(getValues(24, 1, 24, 1)![0][0]!.v).toBe('A25');
@@ -1171,295 +1154,3 @@ describe('Test clipboard', () => {
});
});
});
-
-function createFormulaClipboardWorkbookData(): IWorkbookData {
- return {
- id: 'test',
- appVersion: '3.0.0-alpha',
- locale: LocaleType.ZH_CN,
- name: '',
- sheetOrder: ['sheet1', 'sheet2'],
- styles: {},
- sheets: {
- sheet1: {
- id: 'sheet1',
- name: 'Sheet1',
- cellData: {
- 0: {
- 0: { v: 1 },
- 1: { v: 2 },
- 2: { f: '=A1' },
- 3: { f: '=SUM(A1:B2)' },
- 4: { f: '=$A1' },
- 5: { f: '=A$1' },
- 6: { f: '=$A$1' },
- 7: { f: '=B1' },
- 8: { f: '=H1' },
- 9: { f: '=F3' },
- 10: { f: '=A1', si: 'shared-ref' },
- },
- 1: {
- 0: { v: 3 },
- 1: { v: 4 },
- 10: { si: 'shared-ref' },
- },
- 2: {
- 0: { v: 'label' },
- 1: { v: 7 },
- 2: { f: '=B3' },
- 3: { f: '=C3+1' },
- 4: { v: 11 },
- 5: { f: '=SUM(B3:E3)' },
- },
- },
- },
- sheet2: {
- id: 'sheet2',
- name: 'Sheet2',
- cellData: {
- 0: {
- 0: { f: '=Sheet1!A1' },
- 1: { f: '=Sheet1!H1' },
- },
- },
- },
- },
- };
-}
-
-describe('Test cut command with formulas', () => {
- let univer: Univer;
- let get: Injector['get'];
- let commandService: ICommandService;
- let sheetClipboardService: ISheetClipboardService;
- let getValues: (
- startRow: number,
- startColumn: number,
- endRow: number,
- endColumn: number,
- sheetId?: string
- ) => Array<Array<Nullable<ICellData>>> | undefined;
-
- beforeEach(() => {
- const testBed = clipboardTestBed(createFormulaClipboardWorkbookData(), [
- [UpdateFormulaController],
- ]);
-
- univer = testBed.univer;
- get = testBed.get;
- commandService = get(ICommandService);
- commandService.registerCommand(SetRangeValuesMutation);
- commandService.registerCommand(SetSelectionsOperation);
- commandService.registerCommand(MoveRangeMutation);
- commandService.registerCommand(SetWorksheetRowAutoHeightMutation);
- commandService.registerCommand(SetFormulaDataMutation);
- commandService.registerCommand(SetArrayFormulaDataMutation);
- sheetClipboardService = get(ISheetClipboardService);
-
- get(UpdateFormulaController);
-
- getValues = (
- startRow: number,
- startColumn: number,
- endRow: number,
- endColumn: number,
- sheetId: string = 'sheet1'
- ): Array<Array<Nullable<ICellData>>> | undefined =>
- get(IUniverInstanceService)
- .getUniverSheetInstance('test')
- ?.getSheetBySheetId(sheetId)
- ?.getRange(startRow, startColumn, endRow, endColumn)
- .getValues();
- });
-
- afterEach(() => {
- univer.dispose();
- });
-
- async function cutPaste(
- fromRange: { rows: number[]; cols: number[] },
- toRange: { startRow: number; startColumn: number; endRow: number; endColumn: number },
- fromSubUnitId: string = 'sheet1',
- toSubUnitId: string = fromSubUnitId
- ) {
- const testSheetClipboardService = sheetClipboardService as ITestSheetClipboardService;
- const copyContentCache = sheetClipboardService.copyContentCache();
- const workbook = get(IUniverInstanceService).getUniverSheetInstance('test');
- const targetWorksheet = workbook?.getSheetBySheetId(toSubUnitId);
-
- if (targetWorksheet) {
- workbook?.setActiveSheet(targetWorksheet);
- }
-
- const { matrixFragment, copyId } = testSheetClipboardService._generateCopyContent(
- 'test',
- fromSubUnitId,
- discreteRangeToRange(fromRange),
- []
- );
-
- copyContentCache.set(copyId, {
- unitId: 'test',
- subUnitId: fromSubUnitId,
- range: fromRange,
- matrix: matrixFragment,
- copyType: COPY_TYPE.CUT,
- });
-
- get(SheetsSelectionsService).addSelections([
- {
- range: { ...toRange, rangeType: RANGE_TYPE.NORMAL },
- primary: null,
- style: null,
- },
- ]);
-
- await testSheetClipboardService._pasteInternal(copyId, PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE);
- }
-
- it('cut-moving a referenced value range updates direct, range, mixed-absolute, cross-sheet, and shared formulas', async () => {
- await cutPaste(
- { rows: [0, 1], cols: [0, 1] },
- { startRow: 3, startColumn: 3, endRow: 4, endColumn: 4 }
- );
-
- expect(getValues(0, 0, 1, 1)).toStrictEqual([
- [null, null],
- [null, null],
- ]);
- expect(getValues(3, 3, 4, 4)).toStrictEqual([
- [{ v: 1 }, { v: 2 }],
- [{ v: 3 }, { v: 4 }],
- ]);
-
- expect(getValues(0, 2, 0, 6)).toStrictEqual([
- [
- { f: '=D4' },
- { f: '=SUM(D4:E5)' },
- { f: '=$D4' },
- { f: '=D$4' },
- { f: '=$D$4' },
- ],
- ]);
- expect(getValues(0, 10, 1, 10)).toStrictEqual([
- [{ f: '=D4' }],
- [{ f: '=D5' }],
- ]);
- expect(getValues(0, 0, 0, 1, 'sheet2')).toStrictEqual([
- [{ f: '=Sheet1!D4' }, { f: '=Sheet1!H1' }],
- ]);
-
- expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
- expect(getValues(0, 2, 0, 6)).toStrictEqual([
- [
- { f: '=A1' },
- { f: '=SUM(A1:B2)' },
- { f: '=$A1' },
- { f: '=A$1' },
- { f: '=$A$1' },
- ],
- ]);
- expect(getValues(0, 10, 1, 10)).toStrictEqual([
- [{ f: '=A1', si: 'shared-ref' }],
- [{ si: 'shared-ref' }],
- ]);
- expect(getValues(0, 0, 0, 1, 'sheet2')).toStrictEqual([
- [{ f: '=Sheet1!A1' }, { f: '=Sheet1!H1' }],
- ]);
- });
-
- it('cut-pasting a referenced value range across sheets adds and removes sheet qualifiers correctly', async () => {
- await cutPaste(
- { rows: [0, 1], cols: [0, 1] },
- { startRow: 2, startColumn: 2, endRow: 3, endColumn: 3 },
- 'sheet1',
- 'sheet2'
- );
-
- expect(getValues(0, 0, 1, 1)).toStrictEqual([
- [null, null],
- [null, null],
- ]);
- expect(getValues(2, 2, 3, 3, 'sheet2')).toStrictEqual([
- [{ v: 1 }, { v: 2 }],
- [{ v: 3 }, { v: 4 }],
- ]);
-
- expect(getValues(0, 2, 0, 6)).toStrictEqual([
- [
- { f: '=Sheet2!C3' },
- { f: '=SUM(Sheet2!C3:D4)' },
- { f: '=Sheet2!$C3' },
- { f: '=Sheet2!C$3' },
- { f: '=Sheet2!$C$3' },
- ],
- ]);
- expect(getValues(0, 0, 0, 1, 'sheet2')).toStrictEqual([
- [{ f: '=C3' }, { f: '=Sheet1!H1' }],
- ]);
- });
-
- it('cut-moving a formula cell keeps its formula text stable while dependents update to the new address', async () => {
- await cutPaste(
- { rows: [0], cols: [7] },
- { startRow: 3, startColumn: 9, endRow: 3, endColumn: 9 }
- );
-
- expect(getValues(0, 7, 0, 8)).toStrictEqual([
- [null, { f: '=J4' }],
- ]);
- expect(getValues(3, 9, 3, 9)).toStrictEqual([
- [{ f: '=B1' }],
- ]);
- expect(getValues(0, 0, 0, 1, 'sheet2')).toStrictEqual([
- [{ f: '=Sheet1!A1' }, { f: '=Sheet1!J4' }],
- ]);
-
- expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
- expect(getValues(0, 7, 0, 8)).toStrictEqual([
- [{ f: '=B1' }, { f: '=H1' }],
- ]);
- expect(getValues(0, 0, 0, 1, 'sheet2')).toStrictEqual([
- [{ f: '=Sheet1!A1' }, { f: '=Sheet1!H1' }],
- ]);
- });
-
- it('cut-moving a larger mixed value, text, and formula range preserves moved formulas and updates dependents', async () => {
- await cutPaste(
- { rows: [2], cols: [0, 1, 2, 3, 4, 5] },
- { startRow: 5, startColumn: 0, endRow: 5, endColumn: 5 }
- );
-
- expect(getValues(2, 0, 2, 5)).toStrictEqual([
- [null, null, null, null, null, null],
- ]);
- expect(getValues(5, 0, 5, 5)).toStrictEqual([
- [
- { v: 'label' },
- { v: 7 },
- { f: '=B6' },
- { f: '=C6+1' },
- { v: 11 },
- { f: '=SUM(B6:E6)' },
- ],
- ]);
- expect(getValues(0, 9, 0, 9)).toStrictEqual([
- [{ f: '=F6' }],
- ]);
-
- expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
- expect(getValues(2, 0, 2, 5)).toStrictEqual([
- [
- { v: 'label' },
- { v: 7 },
- { f: '=B3' },
- { f: '=C3+1' },
- { v: 11 },
- { f: '=SUM(B3:E3)' },
- ],
- ]);
- expect(getValues(0, 9, 0, 9)).toStrictEqual([
- [{ f: '=F3' }],
- ]);
- });
-});
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 021c3cc2fc..17b6c61275 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -2623,9 +2623,6 @@ importers:
'@univerjs-infra/shared':
specifier: workspace:*
version: link:../../common/shared
- '@univerjs/sheets-formula':
- specifier: workspace:*
- version: link:../sheets-formula
'@univerjs/themes':
specifier: workspace:*
version: link:../themes