diff --git a/CHANGELOG.md b/CHANGELOG.md index a309672a..8a096529 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -80,6 +80,8 @@ and this project adheres to [Semantic Versioning](https://semver.org). Nor is this a perfect solution, as there may still be issues when function calls have array arguments that themselves contain function calls; but it's still better than the current logic. - Fix for escaping double quotes within a formula [Issue #1971](https://github.com/PHPOffice/PhpSpreadsheet/issues/1971) [PR #2651](https://github.com/PHPOffice/PhpSpreadsheet/pull/2651) +- Fix invalid style of cells in empty columns with columnDimensions and rows with rowDimensions in added external sheet. [PR #2739](https://github.com/PHPOffice/PhpSpreadsheet/pull/2739) + ## 1.22.0 - 2022-02-18 diff --git a/src/PhpSpreadsheet/Spreadsheet.php b/src/PhpSpreadsheet/Spreadsheet.php index 52d7fb55..33b4fe0c 100644 --- a/src/PhpSpreadsheet/Spreadsheet.php +++ b/src/PhpSpreadsheet/Spreadsheet.php @@ -869,6 +869,19 @@ class Spreadsheet $cell->setXfIndex($cell->getXfIndex() + $countCellXfs); } + // update the column dimensions Xfs + foreach ($worksheet->getColumnDimensions() as $columnDimension) { + $columnDimension->setXfIndex($columnDimension->getXfIndex() + $countCellXfs); + } + + // update the row dimensions Xfs + foreach ($worksheet->getRowDimensions() as $rowDimension) { + $xfIndex = $rowDimension->getXfIndex(); + if ($xfIndex !== null) { + $rowDimension->setXfIndex($xfIndex + $countCellXfs); + } + } + return $this->addSheet($worksheet, $sheetIndex); } diff --git a/tests/PhpSpreadsheetTests/SpreadsheetTest.php b/tests/PhpSpreadsheetTests/SpreadsheetTest.php index 4a1b7393..11fb56e4 100644 --- a/tests/PhpSpreadsheetTests/SpreadsheetTest.php +++ b/tests/PhpSpreadsheetTests/SpreadsheetTest.php @@ -188,4 +188,62 @@ class SpreadsheetTest extends TestCase $sheet->getCell('A1')->getStyle()->getFont()->setBold(true); $this->object->addExternalSheet($sheet); } + + public function testAddExternalColumnDimensionStyles(): void + { + $spreadsheet1 = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); + $sheet1 = $spreadsheet1->createSheet()->setTitle('sheetWithColumnDimension'); + $sheet1->getCell('A1')->setValue(1); + $sheet1->getCell('A1')->getStyle()->getFont()->setItalic(true); + $sheet1->getColumnDimension('B')->setWidth(10)->setXfIndex($sheet1->getCell('A1')->getXfIndex()); + $index = $sheet1->getColumnDimension('B')->getXfIndex(); + self::assertEquals(1, $index); + self::assertCount(2, $spreadsheet1->getCellXfCollection()); + + $spreadsheet2 = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); + $sheet2 = $spreadsheet2->createSheet()->setTitle('sheetWithTwoStyles'); + $sheet2->getCell('A1')->setValue(1); + $sheet2->getCell('A1')->getStyle()->getFont()->setBold(true); + $sheet2->getCell('B2')->getStyle()->getFont()->setSuperscript(true); + $countXfs = count($spreadsheet2->getCellXfCollection()); + self::assertEquals(3, $countXfs); + + $sheet3 = $spreadsheet2->addExternalSheet($sheet1); + self::assertCount(5, $spreadsheet2->getCellXfCollection()); + self::assertTrue($sheet3->getCell('A1')->getStyle()->getFont()->getItalic()); + self::assertTrue($sheet3->getCell('B1')->getStyle()->getFont()->getItalic()); + self::assertFalse($sheet3->getCell('B1')->getStyle()->getFont()->getBold()); + // Prove Xf index changed although style is same. + self::assertEquals($countXfs + $index, $sheet3->getCell('B1')->getXfIndex()); + self::assertEquals($countXfs + $index, $sheet3->getColumnDimension('B')->getXfIndex()); + } + + public function testAddExternalRowDimensionStyles(): void + { + $spreadsheet1 = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); + $sheet1 = $spreadsheet1->createSheet()->setTitle('sheetWithColumnDimension'); + $sheet1->getCell('A1')->setValue(1); + $sheet1->getCell('A1')->getStyle()->getFont()->setItalic(true); + $sheet1->getRowDimension(2)->setXfIndex($sheet1->getCell('A1')->getXfIndex()); + $index = $sheet1->getRowDimension(2)->getXfIndex(); + self::assertEquals(1, $index); + self::assertCount(2, $spreadsheet1->getCellXfCollection()); + + $spreadsheet2 = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); + $sheet2 = $spreadsheet2->createSheet()->setTitle('sheetWithTwoStyles'); + $sheet2->getCell('A1')->setValue(1); + $sheet2->getCell('A1')->getStyle()->getFont()->setBold(true); + $sheet2->getCell('B2')->getStyle()->getFont()->setSuperscript(true); + $countXfs = count($spreadsheet2->getCellXfCollection()); + self::assertEquals(3, $countXfs); + + $sheet3 = $spreadsheet2->addExternalSheet($sheet1); + self::assertCount(5, $spreadsheet2->getCellXfCollection()); + self::assertTrue($sheet3->getCell('A1')->getStyle()->getFont()->getItalic()); + self::assertTrue($sheet3->getCell('A2')->getStyle()->getFont()->getItalic()); + self::assertFalse($sheet3->getCell('A2')->getStyle()->getFont()->getBold()); + // Prove Xf index changed although style is same. + self::assertEquals($countXfs + $index, $sheet3->getCell('A2')->getXfIndex()); + self::assertEquals($countXfs + $index, $sheet3->getRowDimension(2)->getXfIndex()); + } }