mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-22 08:09:15 +00:00
Optimize applyFromArray by caching existing styles (#1785)
Prevent calling clone and getHashCode when not needed because these calls are very expensive. When applying styles to a range of cells can we cache the styles we encounter along the way so we don't need to look them up with getHashCode later.
This commit is contained in:
@@ -55,6 +55,29 @@ class StyleTest extends TestCase
|
||||
self::assertFalse($sheet->getStyle('C3')->getFont()->getItalic());
|
||||
}
|
||||
|
||||
public function testStyleIsReused(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
$styleArray = [
|
||||
'font' => [
|
||||
'italic' => true,
|
||||
],
|
||||
];
|
||||
|
||||
$sheet->getStyle('A1')->getFont()->setBold(true);
|
||||
$sheet->getStyle('A2')->getFont()->setBold(true);
|
||||
$sheet->getStyle('A3')->getFont()->setBold(true);
|
||||
$sheet->getStyle('A3')->getFont()->setItalic(true);
|
||||
|
||||
$sheet->getStyle('A')->applyFromArray($styleArray);
|
||||
|
||||
self::assertCount(4, $spreadsheet->getCellXfCollection());
|
||||
$spreadsheet->garbageCollect();
|
||||
|
||||
self::assertCount(3, $spreadsheet->getCellXfCollection());
|
||||
}
|
||||
|
||||
public function testStyleRow(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
|
||||
Reference in New Issue
Block a user