Skip to content

Commit 4856376

Browse files
authored
Merge pull request #2739 from mjan4175/fix-external-sheet-column-styles
Fix invalid styles in empty columns and rows of added external sheet.
2 parents a6cb80f + 2f52cd8 commit 4856376

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ and this project adheres to [Semantic Versioning](https://semver.org).
7979

8080
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.
8181
- 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)
82+
- 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)
83+
8284

8385
## 1.22.0 - 2022-02-18
8486

src/PhpSpreadsheet/Spreadsheet.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,19 @@ public function addExternalSheet(Worksheet $worksheet, $sheetIndex = null)
869869
$cell->setXfIndex($cell->getXfIndex() + $countCellXfs);
870870
}
871871

872+
// update the column dimensions Xfs
873+
foreach ($worksheet->getColumnDimensions() as $columnDimension) {
874+
$columnDimension->setXfIndex($columnDimension->getXfIndex() + $countCellXfs);
875+
}
876+
877+
// update the row dimensions Xfs
878+
foreach ($worksheet->getRowDimensions() as $rowDimension) {
879+
$xfIndex = $rowDimension->getXfIndex();
880+
if ($xfIndex !== null) {
881+
$rowDimension->setXfIndex($xfIndex + $countCellXfs);
882+
}
883+
}
884+
872885
return $this->addSheet($worksheet, $sheetIndex);
873886
}
874887

tests/PhpSpreadsheetTests/SpreadsheetTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,62 @@ public function testAddExternalDuplicateName(): void
188188
$sheet->getCell('A1')->getStyle()->getFont()->setBold(true);
189189
$this->object->addExternalSheet($sheet);
190190
}
191+
192+
public function testAddExternalColumnDimensionStyles(): void
193+
{
194+
$spreadsheet1 = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
195+
$sheet1 = $spreadsheet1->createSheet()->setTitle('sheetWithColumnDimension');
196+
$sheet1->getCell('A1')->setValue(1);
197+
$sheet1->getCell('A1')->getStyle()->getFont()->setItalic(true);
198+
$sheet1->getColumnDimension('B')->setWidth(10)->setXfIndex($sheet1->getCell('A1')->getXfIndex());
199+
$index = $sheet1->getColumnDimension('B')->getXfIndex();
200+
self::assertEquals(1, $index);
201+
self::assertCount(2, $spreadsheet1->getCellXfCollection());
202+
203+
$spreadsheet2 = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
204+
$sheet2 = $spreadsheet2->createSheet()->setTitle('sheetWithTwoStyles');
205+
$sheet2->getCell('A1')->setValue(1);
206+
$sheet2->getCell('A1')->getStyle()->getFont()->setBold(true);
207+
$sheet2->getCell('B2')->getStyle()->getFont()->setSuperscript(true);
208+
$countXfs = count($spreadsheet2->getCellXfCollection());
209+
self::assertEquals(3, $countXfs);
210+
211+
$sheet3 = $spreadsheet2->addExternalSheet($sheet1);
212+
self::assertCount(5, $spreadsheet2->getCellXfCollection());
213+
self::assertTrue($sheet3->getCell('A1')->getStyle()->getFont()->getItalic());
214+
self::assertTrue($sheet3->getCell('B1')->getStyle()->getFont()->getItalic());
215+
self::assertFalse($sheet3->getCell('B1')->getStyle()->getFont()->getBold());
216+
// Prove Xf index changed although style is same.
217+
self::assertEquals($countXfs + $index, $sheet3->getCell('B1')->getXfIndex());
218+
self::assertEquals($countXfs + $index, $sheet3->getColumnDimension('B')->getXfIndex());
219+
}
220+
221+
public function testAddExternalRowDimensionStyles(): void
222+
{
223+
$spreadsheet1 = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
224+
$sheet1 = $spreadsheet1->createSheet()->setTitle('sheetWithColumnDimension');
225+
$sheet1->getCell('A1')->setValue(1);
226+
$sheet1->getCell('A1')->getStyle()->getFont()->setItalic(true);
227+
$sheet1->getRowDimension(2)->setXfIndex($sheet1->getCell('A1')->getXfIndex());
228+
$index = $sheet1->getRowDimension(2)->getXfIndex();
229+
self::assertEquals(1, $index);
230+
self::assertCount(2, $spreadsheet1->getCellXfCollection());
231+
232+
$spreadsheet2 = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
233+
$sheet2 = $spreadsheet2->createSheet()->setTitle('sheetWithTwoStyles');
234+
$sheet2->getCell('A1')->setValue(1);
235+
$sheet2->getCell('A1')->getStyle()->getFont()->setBold(true);
236+
$sheet2->getCell('B2')->getStyle()->getFont()->setSuperscript(true);
237+
$countXfs = count($spreadsheet2->getCellXfCollection());
238+
self::assertEquals(3, $countXfs);
239+
240+
$sheet3 = $spreadsheet2->addExternalSheet($sheet1);
241+
self::assertCount(5, $spreadsheet2->getCellXfCollection());
242+
self::assertTrue($sheet3->getCell('A1')->getStyle()->getFont()->getItalic());
243+
self::assertTrue($sheet3->getCell('A2')->getStyle()->getFont()->getItalic());
244+
self::assertFalse($sheet3->getCell('A2')->getStyle()->getFont()->getBold());
245+
// Prove Xf index changed although style is same.
246+
self::assertEquals($countXfs + $index, $sheet3->getCell('A2')->getXfIndex());
247+
self::assertEquals($countXfs + $index, $sheet3->getRowDimension(2)->getXfIndex());
248+
}
191249
}

0 commit comments

Comments
 (0)