Skip to content

Commit 6fbf474

Browse files
kevin-verschaeveKevin Verschaeve
andauthored
fix: Load tables when flag IReader::READ_DATA_ONLY is used (#3726)
* 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]>
1 parent 1b01b7d commit 6fbf474

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,9 +972,10 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
972972

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

977+
$this->readTables($xmlSheetNS, $docSheet, $dir, $fileWorksheet, $zip, $mainNS);
978+
978979
if ($xmlSheetNS && $xmlSheetNS->mergeCells && $xmlSheetNS->mergeCells->mergeCell && !$this->readDataOnly) {
979980
foreach ($xmlSheetNS->mergeCells->mergeCell as $mergeCellx) {
980981
/** @scrutinizer ignore-call */

tests/PhpSpreadsheetTests/Reader/Xlsx/XlsxTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
88
use PhpOffice\PhpSpreadsheet\IOFactory;
9+
use PhpOffice\PhpSpreadsheet\Reader\IReader;
910
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
1011
use PhpOffice\PhpSpreadsheet\Shared\File;
1112
use PhpOffice\PhpSpreadsheet\Style\Conditional;
@@ -245,4 +246,30 @@ public static function providerStripsWhiteSpaceFromStyleString(): array
245246
height:13.5pt;z-index:5;mso-wrap-style:tight'],
246247
];
247248
}
249+
250+
public function testLoadDataOnlyLoadsAlsoTables(): void
251+
{
252+
$filename = 'tests/data/Reader/XLSX/data_with_tables.xlsx';
253+
$reader = new Xlsx();
254+
$excel = $reader->load($filename, IReader::READ_DATA_ONLY);
255+
256+
self::assertEquals(['First', 'Second'], $excel->getSheetNames());
257+
258+
$table = $excel->getTableByName('Tableau1');
259+
$firstSheet = $excel->getSheetByName('First');
260+
$secondSheet = $excel->getSheetByName('Second');
261+
if (!$table || !$firstSheet || !$secondSheet) {
262+
self::fail('Table or Sheet not found.');
263+
}
264+
265+
self::assertEquals('A1:B5', $table->getRange());
266+
self::assertEquals([['1', '2', '3']], $firstSheet->toArray());
267+
self::assertEquals([
268+
['Colonne1', 'Colonne2'],
269+
['a', 'b'],
270+
['c', 'd'],
271+
['e', 'f'],
272+
['g', 'h'],
273+
], $secondSheet->toArray());
274+
}
248275
}
10 KB
Binary file not shown.

0 commit comments

Comments
 (0)