Skip to content

Commit 2454696

Browse files
committed
Swapped Row and Column Indexes in ReferenceHelper
Fix #4246. This can cause an Exception in unusual circumstances.
1 parent f37b119 commit 2454696

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/PhpSpreadsheet/ReferenceHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,7 @@ private function duplicateStylesByRow(Worksheet $worksheet, int $beforeColumn, i
12151215
if ($worksheet->cellExists($coordinate)) {
12161216
$xfIndex = $worksheet->getCell($coordinate)->getXfIndex();
12171217
for ($j = $beforeRow; $j <= $beforeRow - 1 + $numberOfRows; ++$j) {
1218-
if (!empty($xfIndex) || $worksheet->cellExists([$j, $i])) {
1218+
if (!empty($xfIndex) || $worksheet->cellExists([$i, $j])) {
12191219
$worksheet->getCell(Coordinate::stringFromColumnIndex($i) . $j)->setXfIndex($xfIndex);
12201220
}
12211221
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests;
6+
7+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class ReferenceHelper5Test extends TestCase
11+
{
12+
public function testIssue4246(): void
13+
{
14+
// code below would have thrown exception because
15+
// row and column were swapped in code
16+
$spreadsheet = new Spreadsheet();
17+
$sheet = $spreadsheet->getActiveSheet();
18+
$row = 987654;
19+
$rowMinus1 = $row - 1;
20+
$rowPlus1 = $row + 1;
21+
$sheet->getCell("A$rowMinus1")->setValue(1);
22+
$sheet->getCell("B$rowMinus1")->setValue(2);
23+
$sheet->getCell("C$rowMinus1")->setValue(3);
24+
$sheet->getStyle("A$rowMinus1")->getFont()->setBold(true);
25+
$sheet->getCell("A$row")->setValue(1);
26+
$sheet->getCell("B$row")->setValue(2);
27+
$sheet->getCell("C$row")->setValue(3);
28+
$sheet->getStyle("B$row")->getFont()->setBold(true);
29+
30+
$sheet->insertNewRowBefore($row);
31+
self::assertTrue(
32+
$sheet->getStyle("A$row")->getFont()->getBold()
33+
);
34+
self::assertFalse(
35+
$sheet->getStyle("B$row")->getFont()->getBold()
36+
);
37+
self::assertFalse(
38+
$sheet->getStyle("A$rowPlus1")->getFont()->getBold()
39+
);
40+
self::assertTrue(
41+
$sheet->getStyle("B$rowPlus1")->getFont()->getBold()
42+
);
43+
$spreadsheet->disconnectWorksheets();
44+
}
45+
}

0 commit comments

Comments
 (0)