diff --git a/samples/Reader/22_Reader_issue1767.php b/samples/Reader/22_Reader_issue1767.php new file mode 100644 index 0000000000..2c1ce93143 --- /dev/null +++ b/samples/Reader/22_Reader_issue1767.php @@ -0,0 +1,21 @@ +log('Start'); + +$inputFileType = 'Xlsx'; +$inputFileName = __DIR__ . '/sampleData/issue.1767.xlsx'; + +$helper->log('Loading file ' . $inputFileName . ' using IOFactory with a defined reader type of ' . $inputFileType); +$reader = IOFactory::createReader($inputFileType); +$helper->log('Loading all WorkSheets'); +$reader->setLoadAllSheets(); +$spreadsheet = $reader->load($inputFileName); + +// Save +$helper->write($spreadsheet, __FILE__); + +$helper->log('end'); diff --git a/samples/Reader/sampleData/issue.1767.xlsx b/samples/Reader/sampleData/issue.1767.xlsx new file mode 100644 index 0000000000..9456ab18a1 Binary files /dev/null and b/samples/Reader/sampleData/issue.1767.xlsx differ diff --git a/src/PhpSpreadsheet/Writer/Xlsx.php b/src/PhpSpreadsheet/Writer/Xlsx.php index e229873ebe..e7bb4fea09 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx.php +++ b/src/PhpSpreadsheet/Writer/Xlsx.php @@ -431,8 +431,9 @@ public function save($filename, int $flags = 0): void foreach ($unparsedLoadedData['sheets'][$sheetCodeName]['Drawings'] as $relId => $drawingXml) { $drawingFile = array_search($relId, $unparsedLoadedData['sheets'][$sheetCodeName]['drawingOriginalIds']); if ($drawingFile !== false) { - $drawingFile = ltrim($drawingFile, '.'); - $zipContent['xl' . $drawingFile] = $drawingXml; + //$drawingFile = ltrim($drawingFile, '.'); + //$zipContent['xl' . $drawingFile] = $drawingXml; + $zipContent['xl/drawings/drawing' . ($i + 1) . '.xml'] = $drawingXml; } } } diff --git a/tests/PhpSpreadsheetTests/Writer/Xlsx/Unparsed2396Test.php b/tests/PhpSpreadsheetTests/Writer/Xlsx/Unparsed2396Test.php new file mode 100644 index 0000000000..92a05efa6a --- /dev/null +++ b/tests/PhpSpreadsheetTests/Writer/Xlsx/Unparsed2396Test.php @@ -0,0 +1,90 @@ +filename !== '') { + unlink($this->filename); + $this->filename = ''; + } + } + + private function getContents(?BaseDrawing $drawing): string + { + $contents = ''; + if ($drawing instanceof Drawing) { + $contents = (string) file_get_contents($drawing->getPath()); + } else { + self::fail('Unexpected null or baseDrawing which is not Drawing'); + } + self::assertNotSame('', $contents); + + return $contents; + } + + // Don't drop image as in issue 2396. + public function testUnparsed2396(): void + { + $sampleFilename = 'tests/data/Writer/XLSX/issue.2396.xlsx'; + $reader = new Reader(); + $excel = $reader->load($sampleFilename); + $outputFilename = $this->filename = File::temporaryFilename(); + $writer = new Writer($excel); + $writer->save($outputFilename); + //$spreadsheet = $this->writeAndReload($excel, 'Xlsx'); + $excel->disconnectWorksheets(); + $reader = new Reader(); + $spreadsheet = $reader->load($outputFilename); + $sheet = $spreadsheet->getSheet(0); + $drawing1 = $sheet->getDrawingCollection(); + self::assertCount(1, $drawing1); + $hash = $this->getContents($drawing1[0]); + + $sheet = $spreadsheet->getSheet(1); + $drawings = $sheet->getDrawingCollection(); + self::assertCount(1, $drawings); + self::assertSame($hash, $this->getContents($drawings[0])); + + $sheet = $spreadsheet->getSheet(2); + $drawings = $sheet->getDrawingCollection(); + self::assertCount(0, $drawings); + //self::assertSame($hash, md5(file_get_contents($drawings[0]->getPath()))); + + $sheet = $spreadsheet->getSheet(3); + $drawings = $sheet->getDrawingCollection(); + self::assertCount(1, $drawings); + self::assertSame($hash, $this->getContents($drawings[0])); + + $sheet = $spreadsheet->getSheet(4); + $drawings = $sheet->getDrawingCollection(); + self::assertCount(0, $drawings); + //self::assertSame($hash, md5(file_get_contents($drawings[0]->getPath()))); + + // The next 2 sheets have 'legacyDrawing', button, and listbox. + // If support is added for those, these tests may need adjustment. + $sheet = $spreadsheet->getSheet(5); + $drawings = $sheet->getDrawingCollection(); + self::assertCount(0, $drawings); + //self::assertSame($hash, md5(file_get_contents($drawings[0]->getPath()))); + + $sheet = $spreadsheet->getSheet(6); + $drawings = $sheet->getDrawingCollection(); + self::assertCount(1, $drawings); + self::assertSame($hash, $this->getContents($drawings[0])); + + $spreadsheet->disconnectWorksheets(); + } +} diff --git a/tests/data/Writer/XLSX/issue.2396.xlsx b/tests/data/Writer/XLSX/issue.2396.xlsx new file mode 100644 index 0000000000..b772be9cd7 Binary files /dev/null and b/tests/data/Writer/XLSX/issue.2396.xlsx differ