Skip to content

Commit

Permalink
fix: Load tables when flag IReader::READ_DATA_ONLY is used (#3726)
Browse files Browse the repository at this point in the history
* fix: Load tables when flag IReader::READ_DATA_ONLY is used

* Add test for table read when IReader:::READ_DATA_ONLY is used

---------

Co-authored-by: Kevin Verschaeve <[email protected]>
  • Loading branch information
kevin-verschaeve and Kevin Verschaeve authored Sep 14, 2023
1 parent 1b01b7d commit 6fbf474
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -972,9 +972,10 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet

if ($this->readDataOnly === false) {
$this->readAutoFilter($xmlSheetNS, $docSheet);
$this->readTables($xmlSheetNS, $docSheet, $dir, $fileWorksheet, $zip, $mainNS);
}

$this->readTables($xmlSheetNS, $docSheet, $dir, $fileWorksheet, $zip, $mainNS);

if ($xmlSheetNS && $xmlSheetNS->mergeCells && $xmlSheetNS->mergeCells->mergeCell && !$this->readDataOnly) {
foreach ($xmlSheetNS->mergeCells->mergeCell as $mergeCellx) {
/** @scrutinizer ignore-call */
Expand Down
27 changes: 27 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/XlsxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\IReader;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
Expand Down Expand Up @@ -245,4 +246,30 @@ public static function providerStripsWhiteSpaceFromStyleString(): array
height:13.5pt;z-index:5;mso-wrap-style:tight'],
];
}

public function testLoadDataOnlyLoadsAlsoTables(): void
{
$filename = 'tests/data/Reader/XLSX/data_with_tables.xlsx';
$reader = new Xlsx();
$excel = $reader->load($filename, IReader::READ_DATA_ONLY);

self::assertEquals(['First', 'Second'], $excel->getSheetNames());

$table = $excel->getTableByName('Tableau1');
$firstSheet = $excel->getSheetByName('First');
$secondSheet = $excel->getSheetByName('Second');
if (!$table || !$firstSheet || !$secondSheet) {
self::fail('Table or Sheet not found.');
}

self::assertEquals('A1:B5', $table->getRange());
self::assertEquals([['1', '2', '3']], $firstSheet->toArray());
self::assertEquals([
['Colonne1', 'Colonne2'],
['a', 'b'],
['c', 'd'],
['e', 'f'],
['g', 'h'],
], $secondSheet->toArray());
}
}
Binary file added tests/data/Reader/XLSX/data_with_tables.xlsx
Binary file not shown.

0 comments on commit 6fbf474

Please sign in to comment.