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.

feat(formula): paste with formula (#631)

Dushusirb0c4f2da5b61
diff --git a/packages/formula/src/controllers/trigger-calculation.controller.ts b/packages/formula/src/controllers/trigger-calculation.controller.ts
index 7ffdce90f8..72cc74953d 100644
--- a/packages/formula/src/controllers/trigger-calculation.controller.ts
+++ b/packages/formula/src/controllers/trigger-calculation.controller.ts
@@ -46,6 +46,8 @@ export class TriggerCalculationController extends Disposable {
this._commandExecutedListener();
this._initialExecuteFormulaProcessListener();
+
+ this._initialExecuteFormula();
}
private _commandExecutedListener() {
@@ -146,4 +148,11 @@ export class TriggerCalculationController extends Disposable {
})
);
}
+
+ private _initialExecuteFormula() {
+ this._commandService.executeCommand(setFormulaCalculationStartMutation.id, {
+ commands: [],
+ forceCalculation: true,
+ });
+ }
}
diff --git a/packages/formula/src/controllers/utils.ts b/packages/formula/src/controllers/utils.ts
index d648328bbd..d8c066d491 100644
--- a/packages/formula/src/controllers/utils.ts
+++ b/packages/formula/src/controllers/utils.ts
@@ -43,6 +43,8 @@ export function offsetFormula<T>(
): IFormulaDataGenerics<T> {
const { id } = command;
+ if (checkFormulaDataNull(formulaData, unitId, sheetId)) return formulaData;
+
const formulaMatrix = new ObjectMatrix(formulaData[unitId][sheetId]);
switch (id) {
@@ -269,6 +271,8 @@ function handleDeleteRangeMoveLeft<T>(
}
export function offsetArrayFormula(arrayFormulaRange: IArrayFormulaRangeType, unitId: string, sheetId: string) {
+ if (checkFormulaDataNull(arrayFormulaRange, unitId, sheetId)) return arrayFormulaRange;
+
const arrayFormulaRangeMatrix = new ObjectMatrix(arrayFormulaRange[unitId][sheetId]);
arrayFormulaRangeMatrix.forValue((row, column, range) => {
const { startRow, startColumn, endRow, endColumn } = range;
@@ -291,3 +295,11 @@ export function offsetArrayFormula(arrayFormulaRange: IArrayFormulaRangeType, un
return arrayFormulaRange;
}
+
+function checkFormulaDataNull<T>(formulaData: IFormulaDataGenerics<T>, unitId: string, sheetId: string) {
+ if (formulaData == null || formulaData[unitId] == null || formulaData[unitId][sheetId] == null) {
+ return true;
+ }
+
+ return false;
+}
diff --git a/packages/sheets-formula/src/commands/commands/formula-clipboard.command.ts b/packages/sheets-formula/src/commands/commands/formula-clipboard.command.ts
new file mode 100644
index 0000000000..bb41544fbd
--- /dev/null
+++ b/packages/sheets-formula/src/commands/commands/formula-clipboard.command.ts
@@ -0,0 +1,16 @@
+import type { ICommand } from '@univerjs/core';
+import { CommandType, ICommandService } from '@univerjs/core';
+import { SheetPasteCommand } from '@univerjs/sheets-ui';
+
+export const SPECIAL_PASTE_FORMULA = 'special-paste-formula';
+
+export const SheetOnlyPasteFormulaCommand: ICommand = {
+ id: 'sheet.command.paste-formula',
+ type: CommandType.COMMAND,
+ handler: async (accessor) => {
+ const commandService = accessor.get(ICommandService);
+ return commandService.executeCommand(SheetPasteCommand.id, {
+ value: SPECIAL_PASTE_FORMULA,
+ });
+ },
+};
diff --git a/packages/sheets-formula/src/commands/operations/insert-function.operation.ts b/packages/sheets-formula/src/commands/operations/insert-function.operation.ts
index 2b0572c6b7..78a843526f 100644
--- a/packages/sheets-formula/src/commands/operations/insert-function.operation.ts
+++ b/packages/sheets-formula/src/commands/operations/insert-function.operation.ts
@@ -5,10 +5,16 @@ import {
getCellValueType,
ICommandService,
IUniverInstanceService,
+ Rectangle,
serializeRange,
Tools,
} from '@univerjs/core';
-import { SelectionManagerService } from '@univerjs/sheets';
+import {
+ getCellAtRowCol,
+ NORMAL_SELECTION_PLUGIN_NAME,
+ SelectionManagerService,
+ SetSelectionsOperation,
+} from '@univerjs/sheets';
import type { IAccessor } from '@wendellhu/redi';
import { IFormulaInputService } from '../../services/formula-input.service';
@@ -33,18 +39,25 @@ export const InsertFunctionOperation: ICommand = {
return false;
}
- const univerInstanceService = accessor.get(IUniverInstanceService);
- const cellMatrix = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getCellMatrix();
+ const workbook = accessor.get(IUniverInstanceService).getCurrentUniverSheetInstance();
+ const worksheet = workbook.getActiveSheet();
+
+ const unitId = workbook.getUnitId();
+ const sheetId = worksheet.getSheetId();
+ const cellMatrix = worksheet.getCellMatrix();
const { value } = params;
const commandService = accessor.get(ICommandService);
- // TODO@Dushusir: no match refRange situation, enter edit mode
-
+ // No match refRange situation, enter edit mode
// In each range, first take the judgment result of the primary position (if there is no primary, take the upper left corner),
// If there is a range, set the formula range directly, and then set the formula id of other positions.
// If the range cannot be found, enter the edit mode.
const list: IInsertFunction[] = [];
+ let editRange: IRange | null = null;
+ let editRow = 0;
+ let editColumn = 0;
+
currentSelections.some((selection) => {
const { range, primary } = selection;
@@ -54,8 +67,9 @@ export const InsertFunctionOperation: ICommand = {
const refRange = findRefRange(cellMatrix, row, column);
if (!refRange) {
- // TODO@Dushusir: set current position
- formulaInputService.inputFormula(`=${value}(`);
+ editRange = range;
+ editRow = row;
+ editColumn = column;
return true;
}
@@ -74,6 +88,39 @@ export const InsertFunctionOperation: ICommand = {
return false;
});
+ if (editRange) {
+ // set current position
+ const destRange = getCellAtRowCol(editRow, editColumn, worksheet);
+
+ const resultRange = {
+ range: Rectangle.clone(editRange),
+ primary: {
+ startRow: destRange.startRow,
+ startColumn: destRange.startColumn,
+ endRow: destRange.endRow,
+ endColumn: destRange.endColumn,
+ actualRow: editRow,
+ actualColumn: editColumn,
+ isMerged: destRange.isMerged,
+ isMergedMainCell: destRange.startRow === editRow && destRange.startColumn === editColumn,
+ },
+ };
+
+ const setSelectionParams = {
+ unitId,
+ sheetId,
+ pluginName: NORMAL_SELECTION_PLUGIN_NAME,
+ selections: [resultRange],
+ };
+ await commandService.executeCommand(SetSelectionsOperation.id, setSelectionParams);
+
+ // TODO@DR-Univer: Maybe setTimeout can be removed
+ setTimeout(() => {
+ // edit cell
+ formulaInputService.inputFormula(`=${value}(`);
+ }, 0);
+ }
+
if (list.length === 0) return false;
return commandService.executeCommand(InsertFunctionCommand.id, {
diff --git a/packages/sheets-formula/src/controllers/__tests__/formula-auto-fill.controller.spec.ts b/packages/sheets-formula/src/controllers/__tests__/formula-auto-fill.controller.spec.ts
index 08cf38bbe1..60eb2e89ef 100644
--- a/packages/sheets-formula/src/controllers/__tests__/formula-auto-fill.controller.spec.ts
+++ b/packages/sheets-formula/src/controllers/__tests__/formula-auto-fill.controller.spec.ts
@@ -34,7 +34,7 @@ const theme = {
colorBlack: '#35322b',
};
-describe('Test clear selection content commands', () => {
+describe('Test auto fill with formula', () => {
let univer: Univer;
let get: Injector['get'];
let commandService: ICommandService;
@@ -83,80 +83,78 @@ describe('Test clear selection content commands', () => {
univer.dispose();
});
- describe('auto fill with formula', () => {
- describe('correct situations', () => {
- it('one cell with formula', async () => {
- const selectionManager = get(SelectionManagerService);
- selectionManager.setCurrentSelection({
- pluginName: NORMAL_SELECTION_PLUGIN_NAME,
- unitId: 'test',
- sheetId: 'sheet1',
- });
- selectionManager.add([
- {
- range: { startRow: 0, startColumn: 1, endRow: 0, endColumn: 1, rangeType: RANGE_TYPE.NORMAL },
- primary: null,
- style: null,
- },
- ]);
-
- (autoFillController as any)._handleFill(
- {
- startColumn: 1,
- endColumn: 1,
- startRow: 0,
- endRow: 0,
- },
- {
- startColumn: 1,
- endColumn: 1,
- startRow: 0,
- endRow: 2,
- }
- );
-
- // values will be in the following format
- // [
- // [ { f: '=SUM(A1)' } ],
- // [ { f: '=SUM(A2)', si: '1ZMZWH' } ],
- // [ { si: '1ZMZWH' } ]
- // ]
- let values = getValues(0, 1, 2, 1);
- let B2 = values && values[1][0];
- let B3 = values && values[2][0];
-
- expect(B2?.f).toStrictEqual('=SUM(A2)');
- expect(B2?.si).toEqual(B3?.si);
-
- // undo
- expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
-
- values = getValues(0, 1, 2, 1);
- B2 = values && values[1][0];
- B3 = values && values[2][0];
-
- expect(B2).toStrictEqual({});
- expect(B3).toStrictEqual({});
-
- // redo
- expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
- values = getValues(0, 1, 2, 1);
- B2 = values && values[1][0];
- B3 = values && values[2][0];
-
- expect(B2?.f).toStrictEqual('=SUM(A2)');
- expect(B2?.si).toEqual(B3?.si);
-
- // Restore the original data
- expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+ describe('correct situations', () => {
+ it('one cell with formula', async () => {
+ const selectionManager = get(SelectionManagerService);
+ selectionManager.setCurrentSelection({
+ pluginName: NORMAL_SELECTION_PLUGIN_NAME,
+ unitId: 'test',
+ sheetId: 'sheet1',
});
+ selectionManager.add([
+ {
+ range: { startRow: 0, startColumn: 1, endRow: 0, endColumn: 1, rangeType: RANGE_TYPE.NORMAL },
+ primary: null,
+ style: null,
+ },
+ ]);
+
+ (autoFillController as any)._handleFill(
+ {
+ startColumn: 1,
+ endColumn: 1,
+ startRow: 0,
+ endRow: 0,
+ },
+ {
+ startColumn: 1,
+ endColumn: 1,
+ startRow: 0,
+ endRow: 2,
+ }
+ );
+
+ // values will be in the following format
+ // [
+ // [ { f: '=SUM(A1)' } ],
+ // [ { f: '=SUM(A2)', si: '1ZMZWH' } ],
+ // [ { si: '1ZMZWH' } ]
+ // ]
+ let values = getValues(0, 1, 2, 1);
+ let B2 = values && values[1][0];
+ let B3 = values && values[2][0];
+
+ expect(B2?.f).toStrictEqual('=SUM(A2)');
+ expect(B2?.si).toEqual(B3?.si);
+
+ // undo
+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+
+ values = getValues(0, 1, 2, 1);
+ B2 = values && values[1][0];
+ B3 = values && values[2][0];
+
+ expect(B2).toStrictEqual({});
+ expect(B3).toStrictEqual({});
+
+ // redo
+ expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
+ values = getValues(0, 1, 2, 1);
+ B2 = values && values[1][0];
+ B3 = values && values[2][0];
+
+ expect(B2?.f).toStrictEqual('=SUM(A2)');
+ expect(B2?.si).toEqual(B3?.si);
+
+ // Restore the original data
+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
});
+ });
- describe('fault situations', () => {
- it('will not apply when there is no selected ranges', async () => {
- const result = await commandService.executeCommand(AutoFillCommand.id);
- expect(result).toBeFalsy();
- });
+ describe('fault situations', () => {
+ it('will not apply when there is no selected ranges', async () => {
+ const result = await commandService.executeCommand(AutoFillCommand.id);
+ expect(result).toBeFalsy();
});
});
});
diff --git a/packages/sheets-formula/src/controllers/__tests__/formula-clipboard.controller.spec.ts b/packages/sheets-formula/src/controllers/__tests__/formula-clipboard.controller.spec.ts
new file mode 100644
index 0000000000..c235a0c276
--- /dev/null
+++ b/packages/sheets-formula/src/controllers/__tests__/formula-clipboard.controller.spec.ts
@@ -0,0 +1,175 @@
+import type { ICellData, IMutationInfo, Nullable, Univer } from '@univerjs/core';
+import { ICommandService, IUniverInstanceService, ObjectMatrix } from '@univerjs/core';
+import { FormulaEngineService } from '@univerjs/engine-formula';
+import { type ISetRangeValuesMutationParams, SetRangeValuesMutation } from '@univerjs/sheets';
+import type { ICellDataWithSpanInfo } from '@univerjs/sheets-ui';
+import { COPY_TYPE, ISelectionRenderService, SelectionRenderService } from '@univerjs/sheets-ui';
+import type { Injector } from '@wendellhu/redi';
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+
+import { getSetCellFormulaMutations } from '../formula-clipboard.controller';
+import { createCommandTestBed } from './create-command-test-bed';
+
+describe('Test paste with formula', () => {
+ let univer: Univer;
+ let get: Injector['get'];
+ let commandService: ICommandService;
+ let formulaEngineService: FormulaEngineService;
+ let getValues: (
+ startRow: number,
+ startColumn: number,
+ endRow: number,
+ endColumn: number
+ ) => Array<Array<Nullable<ICellData>>> | undefined;
+
+ beforeEach(() => {
+ const testBed = createCommandTestBed(undefined, [
+ [ISelectionRenderService, { useClass: SelectionRenderService }],
+ [FormulaEngineService],
+ ]);
+ univer = testBed.univer;
+ get = testBed.get;
+ commandService = get(ICommandService);
+ formulaEngineService = get(FormulaEngineService);
+
+ getValues = (
+ startRow: number,
+ startColumn: number,
+ endRow: number,
+ endColumn: number
+ ): Array<Array<Nullable<ICellData>>> | undefined =>
+ get(IUniverInstanceService)
+ .getUniverSheetInstance('test')
+ ?.getSheetBySheetId('sheet1')
+ ?.getRange(startRow, startColumn, endRow, endColumn)
+ .getValues();
+ });
+
+ afterEach(() => {
+ univer.dispose();
+ });
+
+ describe('correct situations', () => {
+ it('Copy two cells, one of which is the formula', async () => {
+ const workbookId = 'test';
+ const worksheetId = 'sheet1';
+ const range = {
+ startRow: 12,
+ startColumn: 2,
+ endRow: 12,
+ endColumn: 3,
+ rangeType: 0,
+ };
+ const matrix = new ObjectMatrix<ICellDataWithSpanInfo>({
+ '0': {
+ '0': {
+ v: 3,
+ },
+ '1': {
+ v: 2,
+ f: '=SUM(A1)',
+ si: '3e4r5t',
+ },
+ },
+ });
+
+ const accessor = {
+ get,
+ };
+
+ const copyInfo = {
+ copyRange: {
+ startRow: 0,
+ startColumn: 2,
+ endRow: 0,
+ endColumn: 3,
+ rangeType: 0,
+ },
+ copyType: COPY_TYPE.COPY,
+ };
+
+ const result = {
+ undos: [
+ {
+ id: SetRangeValuesMutation.id,
+ params: {
+ workbookId,
+ worksheetId,
+ cellValue: {
+ '12': {
+ '2': {
+ s: null,
+ f: null,
+ si: null,
+ p: null,
+ v: null,
+ m: null,
+ t: null,
+ },
+ '3': {
+ s: null,
+ f: null,
+ si: null,
+ p: null,
+ v: null,
+ m: null,
+ t: null,
+ },
+ },
+ },
+ options: {},
+ },
+ },
+ ],
+ redos: [
+ {
+ id: SetRangeValuesMutation.id,
+ params: {
+ workbookId,
+ worksheetId,
+ cellValue: {
+ '12': {
+ '2': {},
+ '3': {
+ f: '=SUM(A13)',
+ v: null,
+ p: null,
+ },
+ },
+ },
+ },
+ },
+ ],
+ };
+
+ const redoUndoList = getSetCellFormulaMutations(
+ workbookId,
+ worksheetId,
+ range,
+ matrix,
+ accessor,
+ copyInfo,
+ formulaEngineService
+ );
+ removeFormulaId(redoUndoList.redos as Array<IMutationInfo<ISetRangeValuesMutationParams>>);
+ expect(redoUndoList).toStrictEqual(result);
+ });
+ });
+});
+
+function removeFormulaId(redos: Array<IMutationInfo<ISetRangeValuesMutationParams>>) {
+ if (redos.length > 0) {
+ const cellValue = redos[0].params.cellValue;
+ if (cellValue) {
+ Object.keys(cellValue).forEach((rowIndex) => {
+ const row = cellValue[Number(rowIndex)];
+ Object.keys(row).forEach((columnIndex) => {
+ const cell = row[Number(columnIndex)];
+ if (cell?.hasOwnProperty('si')) {
+ delete cell?.si;
+ }
+ });
+ });
+ }
+ }
+}
diff --git a/packages/sheets-formula/src/controllers/formula-clipboard.controller.ts b/packages/sheets-formula/src/controllers/formula-clipboard.controller.ts
new file mode 100644
index 0000000000..b39355af19
--- /dev/null
+++ b/packages/sheets-formula/src/controllers/formula-clipboard.controller.ts
@@ -0,0 +1,201 @@
+import type { ICellData, IMutationInfo, IRange } from '@univerjs/core';
+import {
+ Disposable,
+ isFormulaString,
+ IUniverInstanceService,
+ LifecycleStages,
+ ObjectMatrix,
+ OnLifecycle,
+ Tools,
+} from '@univerjs/core';
+import { FormulaEngineService } from '@univerjs/engine-formula';
+import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';
+import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '@univerjs/sheets';
+import type { ICellDataWithSpanInfo, ISheetClipboardHook } from '@univerjs/sheets-ui';
+import { COPY_TYPE, ISheetClipboardService } from '@univerjs/sheets-ui';
+import type { IAccessor } from '@wendellhu/redi';
+import { Inject, Injector } from '@wendellhu/redi';
+
+import { SPECIAL_PASTE_FORMULA } from '../commands/commands/formula-clipboard.command';
+
+export const DEFAULT_PASTE_FORMULA = 'default-paste-formula';
+
+@OnLifecycle(LifecycleStages.Ready, FormulaClipboardController)
+export class FormulaClipboardController extends Disposable {
+ constructor(
+ @IUniverInstanceService private readonly _currentUniverSheet: IUniverInstanceService,
+ @Inject(FormulaEngineService) private readonly _formulaEngineService: FormulaEngineService,
+ @ISheetClipboardService private readonly _sheetClipboardService: ISheetClipboardService,
+ @Inject(Injector) private readonly _injector: Injector
+ ) {
+ super();
+
+ this._initialize();
+ }
+
+ private _initialize(): void {
+ this._registerClipboardHook();
+ }
+
+ private _registerClipboardHook() {
+ this.disposeWithMe(this._sheetClipboardService.addClipboardHook(this._pasteFormulaHook()));
+ this.disposeWithMe(this._sheetClipboardService.addClipboardHook(this._pasteWithFormulaHook()));
+ }
+
+ private _pasteFormulaHook(): ISheetClipboardHook {
+ const specialPasteFormulaHook: ISheetClipboardHook = {
+ hookName: SPECIAL_PASTE_FORMULA,
+ specialPasteInfo: {
+ label: 'specialPaste.formula',
+ },
+ onPasteCells: (pastedRange, matrix, pasteType, copyInfo) => {
+ const workbook = this._currentUniverSheet.getCurrentUniverSheetInstance();
+ const workbookId = workbook.getUnitId();
+ const worksheetId = workbook.getActiveSheet().getSheetId();
+
+ return this._injector.invoke((accessor) =>
+ getSetCellFormulaMutations(
+ workbookId,
+ worksheetId,
+ pastedRange,
+ matrix,
+ accessor,
+ copyInfo,
+ this._formulaEngineService,
+ true
+ )
+ );
+ },
+ };
+
+ return specialPasteFormulaHook;
+ }
+
+ private _pasteWithFormulaHook(): ISheetClipboardHook {
+ const specialPasteFormulaHook: ISheetClipboardHook = {
+ hookName: DEFAULT_PASTE_FORMULA,
+ onPasteCells: (pastedRange, matrix, pasteType, copyInfo) => {
+ const workbook = this._currentUniverSheet.getCurrentUniverSheetInstance();
+ const workbookId = workbook.getUnitId();
+ const worksheetId = workbook.getActiveSheet().getSheetId();
+
+ return this._injector.invoke((accessor) =>
+ getSetCellFormulaMutations(
+ workbookId,
+ worksheetId,
+ pastedRange,
+ matrix,
+ accessor,
+ copyInfo,
+ this._formulaEngineService
+ )
+ );
+ },
+ };
+
+ return specialPasteFormulaHook;
+ }
+}
+
+export function getSetCellFormulaMutations(
+ workbookId: string,
+ worksheetId: string,
+ range: IRange,
+ matrix: ObjectMatrix<ICellDataWithSpanInfo>,
+ accessor: IAccessor,
+ copyInfo: {
+ copyType: COPY_TYPE;
+ copyRange?: IRange;
+ },
+ formulaEngineService: FormulaEngineService,
+ isSpecialPaste = false
+) {
+ const redoMutationsInfo: IMutationInfo[] = [];
+ const undoMutationsInfo: IMutationInfo[] = [];
+ const { startColumn, startRow } = range;
+
+ const valueMatrix = new ObjectMatrix<ICellData>();
+ const formulaIdMap = new Map<string, string>();
+
+ let copyRowLength = 0;
+ let copyColumnLength = 0;
+
+ let copyRangeStartRow = 0;
+ let copyRangeStartColumn = 0;
+
+ if (copyInfo) {
+ const { copyType, copyRange } = copyInfo;
+ if (copyType === COPY_TYPE.COPY && copyRange) {
+ copyRangeStartRow = copyRange.startRow;
+ copyRangeStartColumn = copyRange.startColumn;
+ copyRowLength = copyRange.endRow - copyRangeStartRow + 1;
+ copyColumnLength = copyRange.endColumn - copyRangeStartColumn + 1;
+ }
+ }
+
+ matrix.forValue((row, col, value) => {
+ const originalFormula = value.f || '';
+ const valueObject: ICellDataWithSpanInfo = {};
+
+ // Paste the formula only, you also need to process some regular values
+ if (isSpecialPaste) {
+ valueObject.v = value.v;
+ }
+
+ if (isFormulaString(originalFormula) && copyRowLength && copyColumnLength) {
+ const rowIndex = row % copyRowLength;
+ const colIndex = col % copyColumnLength;
+
+ const index = `${rowIndex}_${colIndex}`;
+ let formulaId = formulaIdMap.get(index);
+
+ if (!formulaId) {
+ formulaId = Tools.generateRandomId(6);
+ formulaIdMap.set(index, formulaId);
+
+ const offsetX = col + startColumn - (copyRangeStartColumn + colIndex);
+ const offsetY = row + startRow - (copyRangeStartRow + rowIndex);
+ const shiftedFormula = formulaEngineService.moveFormulaRefOffset(originalFormula, offsetX, offsetY);
+
+ valueObject.si = formulaId;
+ valueObject.f = shiftedFormula;
+ valueObject.v = null;
+ valueObject.p = null;
+ } else {
+ // At the beginning of the second formula, set formulaId only
+ valueObject.si = formulaId;
+ valueObject.f = null;
+ valueObject.v = null;
+ valueObject.p = null;
+ }
+ }
+
+ valueMatrix.setValue(row + startRow, col + startColumn, valueObject);
+ });
+ // set cell value and style
+ const setValuesMutation: ISetRangeValuesMutationParams = {
+ workbookId,
+ worksheetId,
+ cellValue: valueMatrix.getData(),
+ };
+
+ redoMutationsInfo.push({
+ id: SetRangeValuesMutation.id,
+ params: setValuesMutation,
+ });
+
+ // undo
+ const undoSetValuesMutation: ISetRangeValuesMutationParams = SetRangeValuesUndoMutationFactory(
+ accessor,
+ setValuesMutation
+ );
+
+ undoMutationsInfo.push({
+ id: SetRangeValuesMutation.id,
+ params: undoSetValuesMutation,
+ });
+ return {
+ undos: undoMutationsInfo,
+ redos: redoMutationsInfo,
+ };
+}
diff --git a/packages/sheets-formula/src/controllers/formula-ui.controller.ts b/packages/sheets-formula/src/controllers/formula-ui.controller.ts
index acb85d7f0c..2b7d88f72c 100644
--- a/packages/sheets-formula/src/controllers/formula-ui.controller.ts
+++ b/packages/sheets-formula/src/controllers/formula-ui.controller.ts
@@ -4,6 +4,7 @@ import { ComponentManager, IMenuService, IShortcutService, IUIController } from
import { Inject, Injector } from '@wendellhu/redi';
import { connectInjector } from '@wendellhu/redi/react-bindings';
+import { SheetOnlyPasteFormulaCommand } from '../commands/commands/formula-clipboard.command';
import { InsertFunctionCommand } from '../commands/commands/insert-function.command';
import { SelectEditorFormulaOperation } from '../commands/operations/editor-formula.operation';
import { HelpFunctionOperation } from '../commands/operations/help-function.operation';
@@ -14,7 +15,7 @@ import { SearchFunctionOperation } from '../commands/operations/search-function.
import { RenderFormulaPromptContent } from '../views/FormulaPromptContainer';
import { MORE_FUNCTIONS_COMPONENT } from '../views/more-functions/interface';
import { MoreFunctions } from '../views/more-functions/MoreFunctions';
-import { InsertFunctionMenuItemFactory, MoreFunctionsMenuItemFactory } from './menu';
+import { InsertFunctionMenuItemFactory, MoreFunctionsMenuItemFactory, PasteFormulaMenuItemFactory } from './menu';
import {
ChangeRefToAbsoluteShortcut,
promptSelectionShortcutItem,
@@ -46,14 +47,17 @@ export class FormulaUIController extends Disposable {
}
private _registerMenus(): void {
- [InsertFunctionMenuItemFactory, MoreFunctionsMenuItemFactory].forEach((factory) => {
- this.disposeWithMe(this._menuService.addMenuItem(this._injector.invoke(factory)));
- });
+ [InsertFunctionMenuItemFactory, MoreFunctionsMenuItemFactory, PasteFormulaMenuItemFactory].forEach(
+ (factory) => {
+ this.disposeWithMe(this._menuService.addMenuItem(this._injector.invoke(factory)));
+ }
+ );
}
private _registerCommands(): void {
[
InsertFunctionCommand,
+ SheetOnlyPasteFormulaCommand,
InsertFunctionOperation,
MoreFunctionsOperation,
SearchFunctionOperation,
diff --git a/packages/sheets-formula/src/controllers/menu.ts b/packages/sheets-formula/src/controllers/menu.ts
index d74c69676f..df531f88f2 100644
--- a/packages/sheets-formula/src/controllers/menu.ts
+++ b/packages/sheets-formula/src/controllers/menu.ts
@@ -1,7 +1,9 @@
+import { PASTE_SPECIAL_MENU_ID } from '@univerjs/sheets-ui';
import type { IMenuItem } from '@univerjs/ui';
import { MenuGroup, MenuItemType, MenuPosition } from '@univerjs/ui';
import type { IAccessor } from '@wendellhu/redi';
+import { SheetOnlyPasteFormulaCommand } from '../commands/commands/formula-clipboard.command';
import { InsertFunctionOperation } from '../commands/operations/insert-function.operation';
import { MoreFunctionsOperation } from '../commands/operations/more-functions.operation';
@@ -51,3 +53,12 @@ export function MoreFunctionsMenuItemFactory(accessor: IAccessor): IMenuItem {
type: MenuItemType.BUTTON,
};
}
+
+export function PasteFormulaMenuItemFactory(accessor: IAccessor): IMenuItem {
+ return {
+ id: SheetOnlyPasteFormulaCommand.id,
+ type: MenuItemType.BUTTON,
+ title: 'formula.operation.pasteFormula',
+ positions: [PASTE_SPECIAL_MENU_ID],
+ };
+}
diff --git a/packages/sheets-formula/src/formula-ui-plugin.ts b/packages/sheets-formula/src/formula-ui-plugin.ts
index 076daa2803..2e3bbb6549 100644
--- a/packages/sheets-formula/src/formula-ui-plugin.ts
+++ b/packages/sheets-formula/src/formula-ui-plugin.ts
@@ -5,6 +5,7 @@ import { Inject, Injector } from '@wendellhu/redi';
import { FORMULA_UI_PLUGIN_NAME } from './common/plugin-name';
import { FormulaAutoFillController } from './controllers/formula-auto-fill.controller';
+import { FormulaClipboardController } from './controllers/formula-clipboard.controller';
import { FormulaInputController } from './controllers/formula-input.controller';
import { FormulaUIController } from './controllers/formula-ui.controller';
import { PromptController } from './controllers/prompt.controller';
@@ -50,6 +51,7 @@ export class FormulaUIPlugin extends Plugin {
[PromptController],
[FormulaInputController],
[FormulaAutoFillController],
+ [FormulaClipboardController],
];
dependencies.forEach((dependency) => this._injector.add(dependency));
diff --git a/packages/sheets-formula/src/locale/en-US.ts b/packages/sheets-formula/src/locale/en-US.ts
index c6abebe27b..879155e0bf 100644
--- a/packages/sheets-formula/src/locale/en-US.ts
+++ b/packages/sheets-formula/src/locale/en-US.ts
@@ -73,5 +73,8 @@ export default {
allFunctions: 'All Functions',
syntax: 'SYNTAX',
},
+ operation: {
+ pasteFormula: 'Paste Formula',
+ },
},
};
diff --git a/packages/sheets-formula/src/locale/zh-CN.ts b/packages/sheets-formula/src/locale/zh-CN.ts
index 06b7f34fb9..6a8f59139b 100644
--- a/packages/sheets-formula/src/locale/zh-CN.ts
+++ b/packages/sheets-formula/src/locale/zh-CN.ts
@@ -76,5 +76,9 @@ export default {
allFunctions: '全部函数',
syntax: '语法',
},
+
+ operation: {
+ pasteFormula: '仅粘贴公式',
+ },
},
};
diff --git a/packages/sheets-ui/src/controllers/menu/menu.ts b/packages/sheets-ui/src/controllers/menu/menu.ts
index 2fe1e086a9..8df135df87 100644
--- a/packages/sheets-ui/src/controllers/menu/menu.ts
+++ b/packages/sheets-ui/src/controllers/menu/menu.ts
@@ -899,7 +899,7 @@ export function PasteMenuItemFactory(): IMenuButtonItem {
};
}
-const PASTE_SPECIAL_MENU_ID = 'sheet.menu.paste-special';
+export const PASTE_SPECIAL_MENU_ID = 'sheet.menu.paste-special';
export function PasteSpacialMenuItemFactory(): IMenuSelectorItem {
return {
id: PASTE_SPECIAL_MENU_ID,
diff --git a/packages/sheets-ui/src/index.ts b/packages/sheets-ui/src/index.ts
index 87520ddb58..807ac2e244 100644
--- a/packages/sheets-ui/src/index.ts
+++ b/packages/sheets-ui/src/index.ts
@@ -1,6 +1,8 @@
export * from './basics';
export { getEditorObject } from './basics/editor/get-editor-object';
export { AutoFillCommand } from './commands/commands/auto-fill.command';
+export { SheetPasteCommand } from './commands/commands/clipboard.command';
+export { SheetCopyCommand } from './commands/commands/clipboard.command';
export { ExpandSelectionCommand, JumpOver, MoveSelectionCommand } from './commands/commands/set-selection.command';
export { SetCellEditVisibleArrowOperation } from './commands/operations/cell-edit.operation';
export { SetScrollOperation } from './commands/operations/scroll.operation';
@@ -8,6 +10,7 @@ export { SetEditorResizeOperation } from './commands/operations/set-editor-resiz
export { SetZoomRatioOperation } from './commands/operations/set-zoom-ratio.operation';
export { RangeSelector } from './components/range-selector/RangeSelector';
export { AutoFillController } from './controllers/auto-fill.controller';
+export { PASTE_SPECIAL_MENU_ID } from './controllers/menu/menu';
export { whenEditorInputFormulaActivated } from './controllers/shortcuts/utils';
export {
getCoordByCell,
@@ -21,6 +24,9 @@ export type { IAutoFillHook, ICopyDataPiece } from './services/auto-fill/type';
export { APPLY_TYPE, DATA_TYPE, type IAutoFillRule } from './services/auto-fill/type';
export { type ICopyDataInTypeIndexInfo } from './services/auto-fill/type';
export { ISheetClipboardService } from './services/clipboard/clipboard.service';
+export { SheetClipboardService } from './services/clipboard/clipboard.service';
+export { type ISheetClipboardHook } from './services/clipboard/type';
+export { type ICellDataWithSpanInfo } from './services/clipboard/type';
export { COPY_TYPE } from './services/clipboard/type';
export { CellEditorManagerService, ICellEditorManagerService } from './services/editor/cell-editor-manager.service';
export { IFormulaEditorManagerService } from './services/editor/formula-editor-manager.service';
@@ -29,6 +35,8 @@ export {
IEditorBridgeService,
type IEditorBridgeServiceVisibleParam,
} from './services/editor-bridge.service';
+export { MarkSelectionService } from './services/mark-selection/mark-selection.service';
+export { IMarkSelectionService } from './services/mark-selection/mark-selection.service';
export { ISelectionRenderService } from './services/selection/selection-render.service';
export { SelectionRenderService } from './services/selection/selection-render.service';
export { SelectionShape } from './services/selection/selection-shape';