From 89edc5b267f9cec689302da5d1040eed592bc1f6 Mon Sep 17 00:00:00 2001 From: Zaytcev Ivan Date: Thu, 4 Nov 2021 19:39:58 +0400 Subject: [PATCH] Bug in shared formulas: non-fixed cells are not updated if the formula has a fixed cell (#2354) Example: right shift shared formula: IF(A$1=0,0,A1/A$1) Expected value: IF(B$1=0,0,B1/B$1) Actual value: IF(B$1=0,0,A1/B$1) Similar behavior is observed when copying formulas vertically. This issue occurs because a fixed and a non-fixed cell hit the same element of the $newCellTokens array by index $cellIndex --- src/PhpSpreadsheet/ReferenceHelper.php | 4 +++- tests/data/ReferenceHelperFormulaUpdates.php | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/PhpSpreadsheet/ReferenceHelper.php b/src/PhpSpreadsheet/ReferenceHelper.php index 31f5ecef..580b92c1 100644 --- a/src/PhpSpreadsheet/ReferenceHelper.php +++ b/src/PhpSpreadsheet/ReferenceHelper.php @@ -717,10 +717,12 @@ class ReferenceHelper $toString = ($match[2] > '') ? $match[2] . '!' : ''; $toString .= $modified3; [$column, $row] = Coordinate::coordinateFromString($match[3]); + $columnAdditionalIndex = $column[0] === '$' ? 1 : 0; + $rowAdditionalIndex = $row[0] === '$' ? 1 : 0; // Max worksheet size is 1,048,576 rows by 16,384 columns in Excel 2007, so our adjustments need to be at least one digit more $column = Coordinate::columnIndexFromString(trim($column, '$')) + 100000; $row = (int) trim($row, '$') + 10000000; - $cellIndex = $row . $column; + $cellIndex = $row . $rowAdditionalIndex . $column . $columnAdditionalIndex; $newCellTokens[$cellIndex] = preg_quote($toString, '/'); $cellTokens[$cellIndex] = '/(?