diff --git a/src/PhpSpreadsheet/Reader/Xlsx.php b/src/PhpSpreadsheet/Reader/Xlsx.php index c89f1df5df..7f29194d9d 100644 --- a/src/PhpSpreadsheet/Reader/Xlsx.php +++ b/src/PhpSpreadsheet/Reader/Xlsx.php @@ -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 */ diff --git a/tests/PhpSpreadsheetTests/Reader/Xlsx/XlsxTest.php b/tests/PhpSpreadsheetTests/Reader/Xlsx/XlsxTest.php index ab5081ccc6..f7b86627c7 100644 --- a/tests/PhpSpreadsheetTests/Reader/Xlsx/XlsxTest.php +++ b/tests/PhpSpreadsheetTests/Reader/Xlsx/XlsxTest.php @@ -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; @@ -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()); + } } diff --git a/tests/data/Reader/XLSX/data_with_tables.xlsx b/tests/data/Reader/XLSX/data_with_tables.xlsx new file mode 100644 index 0000000000..30e4f8a3b1 Binary files /dev/null and b/tests/data/Reader/XLSX/data_with_tables.xlsx differ