Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid styles in empty columns of added external sheet. #2739

Merged
merged 9 commits into from
Apr 13, 2022
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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 in added external sheet. [PR #2739](https://github.com/PHPOffice/PhpSpreadsheet/pull/2739)


## 1.22.0 - 2022-02-18

Expand Down
5 changes: 5 additions & 0 deletions src/PhpSpreadsheet/Spreadsheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,11 @@ public function addExternalSheet(Worksheet $worksheet, $sheetIndex = null)
$cell->setXfIndex($cell->getXfIndex() + $countCellXfs);
}

// update the column dimensions Xfs
foreach ($worksheet->getColumnDimensions() as $columnDimension) {
$columnDimension->setXfIndex($columnDimension->getXfIndex() + $countCellXfs);
}

return $this->addSheet($worksheet, $sheetIndex);
}

Expand Down
29 changes: 29 additions & 0 deletions tests/PhpSpreadsheetTests/SpreadsheetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,33 @@ public function testAddExternalDuplicateName(): void
$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());
}
}