Performance Problem With Some Spreadsheets

This commit is contained in:
oleibman
2024-08-30 12:21:46 -07:00
parent 367c22c67e
commit c11524d1a5
2 changed files with 92 additions and 1 deletions
+15 -1
View File
@@ -318,7 +318,21 @@ class Cell implements Stringable
$this->updateInCollection();
$cellCoordinate = $this->getCoordinate();
self::updateIfCellIsTableHeader($this->getParent()?->getParent(), $this, $oldValue, $value);
$this->getWorksheet()->applyStylesFromArray($cellCoordinate, ['quotePrefix' => $quotePrefix]);
$worksheet = $this->getWorksheet();
$spreadsheet = $worksheet->getParent();
$originalSelected = $worksheet->getSelectedCells();
if (isset($spreadsheet)) {
$activeSheetIndex = $spreadsheet->getActiveSheetIndex();
$style = $this->getStyle();
$oldQuotePrefix = $style->getQuotePrefix();
if ($oldQuotePrefix != $quotePrefix) {
$style->setQuotePrefix($quotePrefix);
}
$worksheet->setSelectedCells($originalSelected);
if ($activeSheetIndex >= 0) {
$spreadsheet->setActiveSheetIndex($activeSheetIndex);
}
}
return $this->getParent()?->get($cellCoordinate) ?? $this;
}
@@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PHPUnit\Framework\TestCase;
class ApplyStylesTest extends TestCase
{
public function testApplyFromArray(): void
{
$spreadsheet = new Spreadsheet();
$sheet1 = $spreadsheet->getActiveSheet();
$sheet2 = $spreadsheet->createSheet();
$sheet3 = $spreadsheet->createSheet();
$cell = 'B4';
$sheet1->getCell($cell)->setValue('first');
$sheet1->getStyle($cell)->getFont()->setName('Arial');
$cell = 'C9';
$sheet2->getCell($cell)->setValue('second');
$sheet2->getStyle($cell)->getFont()->setName('Arial');
$cell = 'A6';
$sheet3->getCell($cell)->setValue('third');
$sheet3->getStyle($cell)->getFont()->setName('Arial');
self::assertSame(2, $spreadsheet->getActiveSheetIndex());
self::assertSame('B4', $sheet1->getSelectedCells());
self::assertSame('C9', $sheet2->getSelectedCells());
self::assertSame('A6', $sheet3->getSelectedCells());
$cell = 'D12';
$styleArray = ['font' => ['name' => 'Courier New']];
$sheet2->getStyle('D12')->applyFromArray($styleArray);
self::assertSame(1, $spreadsheet->getActiveSheetIndex());
self::assertSame('B4', $sheet1->getSelectedCells());
self::assertSame('D12', $sheet2->getSelectedCells());
self::assertSame('A6', $sheet3->getSelectedCells());
$spreadsheet->disconnectWorksheets();
}
public function testApplyStylesFromArray(): void
{
$spreadsheet = new Spreadsheet();
$sheet1 = $spreadsheet->getActiveSheet();
$sheet2 = $spreadsheet->createSheet();
$sheet3 = $spreadsheet->createSheet();
$cell = 'B4';
$sheet1->getCell($cell)->setValue('first');
$sheet1->getStyle($cell)->getFont()->setName('Arial');
$cell = 'C9';
$sheet2->getCell($cell)->setValue('second');
$sheet2->getStyle($cell)->getFont()->setName('Arial');
$cell = 'A6';
$sheet3->getCell($cell)->setValue('third');
$sheet3->getStyle($cell)->getFont()->setName('Arial');
self::assertSame(2, $spreadsheet->getActiveSheetIndex());
self::assertSame('B4', $sheet1->getSelectedCells());
self::assertSame('C9', $sheet2->getSelectedCells());
self::assertSame('A6', $sheet3->getSelectedCells());
$cell = 'D12';
$styleArray = ['font' => ['name' => 'Courier New']];
$sheet2->applyStylesFromArray($cell, ['font' => ['name' => 'Courier New']]);
self::assertSame(2, $spreadsheet->getActiveSheetIndex(), 'should be unchanged');
self::assertSame('B4', $sheet1->getSelectedCells(), 'should be unchanged');
self::assertSame('C9', $sheet2->getSelectedCells(), 'should be unchanged');
self::assertSame('A6', $sheet3->getSelectedCells(), 'should be unchanged');
$spreadsheet->disconnectWorksheets();
}
public function testNoSpreadsheet(): void
{
$sheet2 = new Worksheet();
$cell = 'D12';
self::assertFalse($sheet2->applyStylesFromArray($cell, ['font' => ['name' => 'Courier New']]));
}
}