From 2454696dc60d18341974278834be340376b623fd Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Wed, 27 Nov 2024 18:06:23 -0800 Subject: [PATCH] Swapped Row and Column Indexes in ReferenceHelper Fix #4246. This can cause an Exception in unusual circumstances. --- src/PhpSpreadsheet/ReferenceHelper.php | 2 +- .../ReferenceHelper5Test.php | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 tests/PhpSpreadsheetTests/ReferenceHelper5Test.php diff --git a/src/PhpSpreadsheet/ReferenceHelper.php b/src/PhpSpreadsheet/ReferenceHelper.php index afcfa016ec..dfcea10e45 100644 --- a/src/PhpSpreadsheet/ReferenceHelper.php +++ b/src/PhpSpreadsheet/ReferenceHelper.php @@ -1215,7 +1215,7 @@ private function duplicateStylesByRow(Worksheet $worksheet, int $beforeColumn, i if ($worksheet->cellExists($coordinate)) { $xfIndex = $worksheet->getCell($coordinate)->getXfIndex(); for ($j = $beforeRow; $j <= $beforeRow - 1 + $numberOfRows; ++$j) { - if (!empty($xfIndex) || $worksheet->cellExists([$j, $i])) { + if (!empty($xfIndex) || $worksheet->cellExists([$i, $j])) { $worksheet->getCell(Coordinate::stringFromColumnIndex($i) . $j)->setXfIndex($xfIndex); } } diff --git a/tests/PhpSpreadsheetTests/ReferenceHelper5Test.php b/tests/PhpSpreadsheetTests/ReferenceHelper5Test.php new file mode 100644 index 0000000000..bdb658896e --- /dev/null +++ b/tests/PhpSpreadsheetTests/ReferenceHelper5Test.php @@ -0,0 +1,45 @@ +getActiveSheet(); + $row = 987654; + $rowMinus1 = $row - 1; + $rowPlus1 = $row + 1; + $sheet->getCell("A$rowMinus1")->setValue(1); + $sheet->getCell("B$rowMinus1")->setValue(2); + $sheet->getCell("C$rowMinus1")->setValue(3); + $sheet->getStyle("A$rowMinus1")->getFont()->setBold(true); + $sheet->getCell("A$row")->setValue(1); + $sheet->getCell("B$row")->setValue(2); + $sheet->getCell("C$row")->setValue(3); + $sheet->getStyle("B$row")->getFont()->setBold(true); + + $sheet->insertNewRowBefore($row); + self::assertTrue( + $sheet->getStyle("A$row")->getFont()->getBold() + ); + self::assertFalse( + $sheet->getStyle("B$row")->getFont()->getBold() + ); + self::assertFalse( + $sheet->getStyle("A$rowPlus1")->getFont()->getBold() + ); + self::assertTrue( + $sheet->getStyle("B$rowPlus1")->getFont()->getBold() + ); + $spreadsheet->disconnectWorksheets(); + } +}