Skip to content

Commit 50ec30b

Browse files
authored
Merge pull request #4153 from oleibman/issue4128b
Improve Xlsx Reader Speed
2 parents f023e0d + bba5719 commit 50ec30b

File tree

5 files changed

+96
-6
lines changed

5 files changed

+96
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
4141

4242
- Xls Reader Some Ranges Not Handled Properly. [Issue #1570](https://github.com/PHPOffice/PhpSpreadsheet/issues/1570) [PR #4140](https://github.com/PHPOffice/PhpSpreadsheet/pull/4140)
4343
- Better Handling of legacyDrawing Xml. [Issue #4105](https://github.com/PHPOffice/PhpSpreadsheet/issues/4105) [PR #4122](https://github.com/PHPOffice/PhpSpreadsheet/pull/4122)
44+
- Improve Xlsx Reader Speed. [Issue #3917](https://github.com/PHPOffice/PhpSpreadsheet/issues/3917) [PR #4153](https://github.com/PHPOffice/PhpSpreadsheet/pull/4153)
4445

4546
## 2024-08-07 - 2.2.2
4647

src/PhpSpreadsheet/Cell/Cell.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,21 @@ public function setValueExplicit(mixed $value, string $dataType = DataType::TYPE
318318
$this->updateInCollection();
319319
$cellCoordinate = $this->getCoordinate();
320320
self::updateIfCellIsTableHeader($this->getParent()?->getParent(), $this, $oldValue, $value);
321-
$this->getWorksheet()->applyStylesFromArray($cellCoordinate, ['quotePrefix' => $quotePrefix]);
321+
$worksheet = $this->getWorksheet();
322+
$spreadsheet = $worksheet->getParent();
323+
if (isset($spreadsheet)) {
324+
$originalSelected = $worksheet->getSelectedCells();
325+
$activeSheetIndex = $spreadsheet->getActiveSheetIndex();
326+
$style = $this->getStyle();
327+
$oldQuotePrefix = $style->getQuotePrefix();
328+
if ($oldQuotePrefix !== $quotePrefix) {
329+
$style->setQuotePrefix($quotePrefix);
330+
}
331+
$worksheet->setSelectedCells($originalSelected);
332+
if ($activeSheetIndex >= 0) {
333+
$spreadsheet->setActiveSheetIndex($activeSheetIndex);
334+
}
335+
}
322336

323337
return $this->getParent()?->get($cellCoordinate) ?? $this;
324338
}

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -950,16 +950,16 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
950950

951951
// Style information?
952952
if (!$this->readDataOnly) {
953-
$holdSelected = $docSheet->getSelectedCells();
954953
$cAttrS = (int) ($cAttr['s'] ?? 0);
955954
// no style index means 0, it seems
956955
$cAttrS = isset($styles[$cAttrS]) ? $cAttrS : 0;
957956
$cell->setXfIndex($cAttrS);
958957
// issue 3495
959958
if ($cellDataType === DataType::TYPE_FORMULA && $styles[$cAttrS]->quotePrefix === true) {
959+
$holdSelected = $docSheet->getSelectedCells();
960960
$cell->getStyle()->setQuotePrefix(false);
961+
$docSheet->setSelectedCells($holdSelected);
961962
}
962-
$docSheet->setSelectedCells($holdSelected);
963963
}
964964
}
965965
++$rowIndex;

src/PhpSpreadsheet/Worksheet/Worksheet.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3730,10 +3730,8 @@ public function applyStylesFromArray(string $coordinate, array $styleArray): boo
37303730
}
37313731
$activeSheetIndex = $spreadsheet->getActiveSheetIndex();
37323732
$originalSelected = $this->selectedCells;
3733-
$originalActive = $this->activeCell;
37343733
$this->getStyle($coordinate)->applyFromArray($styleArray);
3735-
$this->activeCell = $originalActive;
3736-
$this->selectedCells = $originalSelected;
3734+
$this->setSelectedCells($originalSelected);
37373735
if ($activeSheetIndex >= 0) {
37383736
$spreadsheet->setActiveSheetIndex($activeSheetIndex);
37393737
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
6+
7+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8+
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class ApplyStylesTest extends TestCase
12+
{
13+
public function testApplyFromArray(): void
14+
{
15+
$spreadsheet = new Spreadsheet();
16+
$sheet1 = $spreadsheet->getActiveSheet();
17+
$sheet2 = $spreadsheet->createSheet();
18+
$sheet3 = $spreadsheet->createSheet();
19+
$cell = 'B4';
20+
$sheet1->getCell($cell)->setValue('first');
21+
$sheet1->getStyle($cell)->getFont()->setName('Arial');
22+
$cell = 'C9';
23+
$sheet2->getCell($cell)->setValue('second');
24+
$sheet2->getStyle($cell)->getFont()->setName('Arial');
25+
$cell = 'A6';
26+
$sheet3->getCell($cell)->setValue('third');
27+
$sheet3->getStyle($cell)->getFont()->setName('Arial');
28+
self::assertSame(2, $spreadsheet->getActiveSheetIndex());
29+
self::assertSame('B4', $sheet1->getSelectedCells());
30+
self::assertSame('C9', $sheet2->getSelectedCells());
31+
self::assertSame('A6', $sheet3->getSelectedCells());
32+
$cell = 'D12';
33+
$styleArray = ['font' => ['name' => 'Courier New']];
34+
$sheet2->getStyle($cell)->applyFromArray($styleArray);
35+
self::assertSame(1, $spreadsheet->getActiveSheetIndex());
36+
self::assertSame('B4', $sheet1->getSelectedCells());
37+
self::assertSame('D12', $sheet2->getSelectedCells());
38+
self::assertSame('A6', $sheet3->getSelectedCells());
39+
$spreadsheet->disconnectWorksheets();
40+
}
41+
42+
public function testApplyStylesFromArray(): void
43+
{
44+
$spreadsheet = new Spreadsheet();
45+
$sheet1 = $spreadsheet->getActiveSheet();
46+
$sheet2 = $spreadsheet->createSheet();
47+
$sheet3 = $spreadsheet->createSheet();
48+
$cell = 'B4';
49+
$sheet1->getCell($cell)->setValue('first');
50+
$sheet1->getStyle($cell)->getFont()->setName('Arial');
51+
$cell = 'C9';
52+
$sheet2->getCell($cell)->setValue('second');
53+
$sheet2->getStyle($cell)->getFont()->setName('Arial');
54+
$cell = 'A6';
55+
$sheet3->getCell($cell)->setValue('third');
56+
$sheet3->getStyle($cell)->getFont()->setName('Arial');
57+
self::assertSame(2, $spreadsheet->getActiveSheetIndex());
58+
self::assertSame('B4', $sheet1->getSelectedCells());
59+
self::assertSame('C9', $sheet2->getSelectedCells());
60+
self::assertSame('A6', $sheet3->getSelectedCells());
61+
$cell = 'D12';
62+
$styleArray = ['font' => ['name' => 'Courier New']];
63+
$sheet2->applyStylesFromArray($cell, $styleArray);
64+
self::assertSame(2, $spreadsheet->getActiveSheetIndex(), 'should be unchanged');
65+
self::assertSame('B4', $sheet1->getSelectedCells(), 'should be unchanged');
66+
self::assertSame('C9', $sheet2->getSelectedCells(), 'should be unchanged');
67+
self::assertSame('A6', $sheet3->getSelectedCells(), 'should be unchanged');
68+
$spreadsheet->disconnectWorksheets();
69+
}
70+
71+
public function testNoSpreadsheet(): void
72+
{
73+
$sheet2 = new Worksheet();
74+
$cell = 'D12';
75+
self::assertFalse($sheet2->applyStylesFromArray($cell, ['font' => ['name' => 'Courier New']]));
76+
}
77+
}

0 commit comments

Comments
 (0)