From a64ed1654e81cef892a737bb7ce9e4bc6b7dab75 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Fri, 22 Apr 2022 21:57:56 +0000 Subject: [PATCH] fpspreadsheet: Fix issue when moving cells with formulas mentioned in previous commit. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8263 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../fpspreadsheet/source/common/fpspreadsheet.pas | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/components/fpspreadsheet/source/common/fpspreadsheet.pas b/components/fpspreadsheet/source/common/fpspreadsheet.pas index 3bfda6d4e..aefe0e9f5 100644 --- a/components/fpspreadsheet/source/common/fpspreadsheet.pas +++ b/components/fpspreadsheet/source/common/fpspreadsheet.pas @@ -2276,6 +2276,7 @@ var fromRow, fromCol: Cardinal; sheet: TsWorksheet; i: Integer; + formula: String; begin if ACell = nil then exit; @@ -2289,7 +2290,16 @@ begin fromCol := ACell^.Col; // Copy cell to new location + // Note: In Excel the formula in a moved cell still points to the initial + // location. This is different from copying a formula. + // --> We must prevent CopyCell from adjusting the formula + // --> Erase the formula temporarily. + formula := ReadFormula(ACell); + DeleteFormula(ACell); CopyCell(fromRow, fromCol, AToRow, AToCol); + // Restore the old formula which points to the old location. + if formula <> '' then + WriteFormula(AToRow, AToCol, formula); // Fix formula references to this cell for i := 0 to FWorkbook.GetWorksheetcount-1 do begin