Skip to content

Commit

Permalink
Swapped Row and Column Indexes in ReferenceHelper
Browse files Browse the repository at this point in the history
Fix PHPOffice#4246. This can cause an Exception in unusual circumstances.
  • Loading branch information
oleibman committed Nov 28, 2024
1 parent f37b119 commit 2454696
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/ReferenceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
45 changes: 45 additions & 0 deletions tests/PhpSpreadsheetTests/ReferenceHelper5Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;

class ReferenceHelper5Test extends TestCase
{
public function testIssue4246(): void
{
// code below would have thrown exception because
// row and column were swapped in code
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->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();
}
}

0 comments on commit 2454696

Please sign in to comment.