Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] clipboard: improve cut formula behaviour #4152

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/clipboard_handlers/cell_clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,17 @@ export class CellClipboardHandler extends AbstractCellClipboardHandler<
return;
}

const content =
origin.cell && origin.cell.isFormula && !clipboardOption?.isCutOperation
? this.getters.getTranslatedCellFormula(
sheetId,
col - origin.position.col,
row - origin.position.row,
origin.cell.compiledFormula
)
: origin.cell?.content;
let content = origin.cell?.content;
if (origin.cell?.isFormula && !clipboardOption?.isCutOperation) {
content = this.getters.getTranslatedCellFormula(
sheetId,
col - origin.position.col,
row - origin.position.row,
origin.cell.compiledFormula
);
} else if (origin.cell?.isFormula) {
content = this.getters.getFormulaMovedInSheet(sheetId, origin.cell.compiledFormula);
}
if (content !== "" || origin.cell?.format || origin.cell?.style) {
this.dispatch("UPDATE_CELL", {
...target,
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ export class RangeImpl implements Range {
: this.parts.map((part) => {
return { rowFixed: part.rowFixed, colFixed: part.colFixed };
}),
prefixSheet: rangeParams?.prefixSheet ? rangeParams.prefixSheet : this.prefixSheet,
prefixSheet:
rangeParams?.prefixSheet !== undefined ? rangeParams.prefixSheet : this.prefixSheet,
},
this.getSheetSize
);
Expand Down
1 change: 1 addition & 0 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ export class Model extends EventBus<any> implements CommandDispatcher {
this.coreGetters.isRangeValid = this.range.isRangeValid.bind(this.range);
this.coreGetters.extendRange = this.range.extendRange.bind(this.range);
this.coreGetters.getRangesUnion = this.range.getRangesUnion.bind(this.range);
this.coreGetters.removeRangesSheetPrefix = this.range.removeRangesSheetPrefix.bind(this.range);

this.getters = {
isReadonly: () => this.config.mode === "readonly" || this.config.mode === "dashboard",
Expand Down
7 changes: 7 additions & 0 deletions src/plugins/core/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export class CellPlugin extends CorePlugin<CoreState> implements CoreState {
"getTranslatedCellFormula",
"getCellStyle",
"getCellById",
"getFormulaMovedInSheet",
] as const;
readonly nextId = 1;
public readonly cells: { [sheetId: string]: { [id: string]: Cell } } = {};
Expand Down Expand Up @@ -373,6 +374,12 @@ export class CellPlugin extends CorePlugin<CoreState> implements CoreState {
return this.getFormulaCellContent(sheetId, compiledFormula, adaptedDependencies);
}

getFormulaMovedInSheet(targetSheetId: UID, compiledFormula: RangeCompiledFormula) {
const dependencies = compiledFormula.dependencies;
const adaptedDependencies = this.getters.removeRangesSheetPrefix(targetSheetId, dependencies);
return this.getFormulaCellContent(targetSheetId, compiledFormula, adaptedDependencies);
}

getCellStyle(position: CellPosition): Style {
return this.getters.getCell(position)?.style || {};
}
Expand Down
14 changes: 14 additions & 0 deletions src/plugins/core/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class RangeAdapter implements CommandHandler<CoreCommand> {
"getRangesUnion",
"recomputeRanges",
"isRangeValid",
"removeRangesSheetPrefix",
] as const;

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -292,6 +293,19 @@ export class RangeAdapter implements CommandHandler<CoreCommand> {
});
}

/**
* Remove the sheet name prefix if a range is part of the given sheet.
*/
removeRangesSheetPrefix(sheetId: UID, ranges: Range[]): Range[] {
return ranges.map((range) => {
const rangeImpl = RangeImpl.fromRange(range, this.getters);
if (rangeImpl.prefixSheet && rangeImpl.sheetId === sheetId) {
return rangeImpl.clone({ prefixSheet: false });
}
return rangeImpl;
});
}

extendRange(range: Range, dimension: Dimension, quantity: number): Range {
const rangeImpl = RangeImpl.fromRange(range, this.getters);
const right = dimension === "COL" ? rangeImpl.zone.right + quantity : rangeImpl.zone.right;
Expand Down
13 changes: 13 additions & 0 deletions tests/clipboard/clipboard_plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,19 @@ describe("clipboard", () => {
expect(getCellText(model, "B2")).toBe("=SUM(C1:C2)");
});

test("cut/paste a formula with references in another sheet updates the sheet references in the formula", () => {
const model = new Model();
createSheet(model, { sheetId: "sh2", name: "Sheet2" });
setCellContent(model, "A1", "=SUM(C1:C2)");
setCellContent(model, "B1", "=Sheet2!A1 + A2");
cut(model, "A1:B1");

activateSheet(model, "sh2");
paste(model, "A1");
expect(getCellText(model, "A1")).toBe("=SUM(Sheet1!C1:C2)");
expect(getCellText(model, "B1")).toBe("=A1 + Sheet1!A2");
});

test("copy/paste a zone present in formulas references does not update references", () => {
const model = new Model();
setCellContent(model, "A1", "=B2");
Expand Down