Skip to content

Commit

Permalink
fix(formula): paste formula in other sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
Dushusir committed Jun 11, 2024
1 parent 4199cff commit bcde98f
Showing 1 changed file with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
Tools,
UniverInstanceType,
} from '@univerjs/core';
import { LexerTreeBuilder } from '@univerjs/engine-formula';
import { FormulaDataModel, LexerTreeBuilder } from '@univerjs/engine-formula';
import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';
import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '@univerjs/sheets';
import { COPY_TYPE, ISheetClipboardService, PREDEFINED_HOOK_NAME } from '@univerjs/sheets-ui';
Expand All @@ -44,7 +44,8 @@ export class FormulaClipboardController extends Disposable {
@IUniverInstanceService private readonly _currentUniverSheet: IUniverInstanceService,
@Inject(LexerTreeBuilder) private readonly _lexerTreeBuilder: LexerTreeBuilder,
@ISheetClipboardService private readonly _sheetClipboardService: ISheetClipboardService,
@Inject(Injector) private readonly _injector: Injector
@Inject(Injector) private readonly _injector: Injector,
@Inject(FormulaDataModel) private readonly _formulaDataModel: FormulaDataModel
) {
super();

Expand Down Expand Up @@ -92,8 +93,8 @@ export class FormulaClipboardController extends Disposable {
const pastedRange = pasteTo.range;
const matrix = data;
const workbook = this._currentUniverSheet.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
const unitId = workbook.getUnitId();
const subUnitId = workbook.getActiveSheet().getSheetId();
const unitId = pasteTo.unitId || workbook.getUnitId();
const subUnitId = pasteTo.subUnitId || workbook.getActiveSheet().getSheetId();

return this._injector.invoke((accessor) => getSetCellFormulaMutations(
unitId,
Expand All @@ -103,11 +104,14 @@ export class FormulaClipboardController extends Disposable {
accessor,
copyInfo,
this._lexerTreeBuilder,
isSpecialPaste
this._formulaDataModel,
isSpecialPaste,
pasteFrom
));
}
}

// eslint-disable-next-line max-lines-per-function
export function getSetCellFormulaMutations(
unitId: string,
subUnitId: string,
Expand All @@ -120,7 +124,9 @@ export function getSetCellFormulaMutations(
pasteType: string;
},
lexerTreeBuilder: LexerTreeBuilder,
isSpecialPaste = false
formulaDataModel: FormulaDataModel,
isSpecialPaste = false,
pasteFrom: ISheetDiscreteRangeLocation | null
) {
const redoMutationsInfo: IMutationInfo[] = [];
const undoMutationsInfo: IMutationInfo[] = [];
Expand Down Expand Up @@ -155,10 +161,18 @@ export function getSetCellFormulaMutations(

// Directly reuse when there is a formula id
if (isFormulaId(originalFormulaId)) {
valueObject.si = originalFormulaId;
valueObject.f = null;
valueObject.v = null;
valueObject.p = null;
const { unitId: pasteFromUnitId = '', subUnitId: pasteFromSubUnitId = '', range } = pasteFrom || {};

if (((pasteFromUnitId && unitId !== pasteFromUnitId) || (pasteFromSubUnitId && subUnitId !== pasteFromSubUnitId)) && range?.rows && range?.cols) {
const formulaString = formulaDataModel.getFormulaStringByCell(range.rows[row], range.cols[col], pasteFromSubUnitId, pasteFromUnitId);

// TODO handle as normal formula string
} else {
valueObject.si = originalFormulaId;
valueObject.f = null;
valueObject.v = null;
valueObject.p = null;
}
} else if (isFormulaString(originalFormula) && copyInfo.pasteType === PREDEFINED_HOOK_NAME.DEFAULT_PASTE) {
const rowIndex = row % copyRowLength;
const colIndex = col % copyColumnLength;
Expand Down

0 comments on commit bcde98f

Please sign in to comment.