Skip to content

Commit ea74c96

Browse files
authored
Name Clashes Between Parsed and Unparsed Drawings (#2423)
* Name Clashes Between Parsed and Unparsed Drawings This is at least a partial fix for #2396 and #1767 (which has been around for a long time). PhpSpreadsheet renames drawing XML files when it reads them from a spreadsheet. However, when it writes unparsed drawing files, it uses the original names, which can result in a clash with the renamed files. The solution in this PR is to write the unparsed files using the same renaming convention as the the others. This is an incredibly simple fix, basically a one-line change, for such a long-lived problem. It is conceivable that this PR breaks a more sophisticated file than I have come across, e.g. with multiple unparsed files associated with a single worksheet. However, this PR does fix at least part of the problem for both issues, and causes no regression issues. The changed code was covered in only 2 tests - Reader/XlsxTest testLoadSaveWithEmptyDrawings and Writer/Xlsx/UnparsedDataTest testLoadSaveXlsxWithUnparsedData. 2396 is covered by a new test Unparsed2396Test. I had trouble figuring out what to test for 1767. Since it is a problem that becomes evident only when the output file is opened in Excel, I added a new sample to cover it. * Sloppy Errors I neglected to run php-cs-fixer and phpstan, and it bit me. * Scrutinizer It's not as good as Phpstan at recognizing problems that can't happen due to previous assertions. * Scrutinizer Again It can be really stupid sometimes.
1 parent 5240a23 commit ea74c96

File tree

5 files changed

+114
-2
lines changed

5 files changed

+114
-2
lines changed
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\IOFactory;
4+
5+
require __DIR__ . '/../Header.php';
6+
7+
$helper->log('Start');
8+
9+
$inputFileType = 'Xlsx';
10+
$inputFileName = __DIR__ . '/sampleData/issue.1767.xlsx';
11+
12+
$helper->log('Loading file ' . $inputFileName . ' using IOFactory with a defined reader type of ' . $inputFileType);
13+
$reader = IOFactory::createReader($inputFileType);
14+
$helper->log('Loading all WorkSheets');
15+
$reader->setLoadAllSheets();
16+
$spreadsheet = $reader->load($inputFileName);
17+
18+
// Save
19+
$helper->write($spreadsheet, __FILE__);
20+
21+
$helper->log('end');
60.9 KB
Binary file not shown.

src/PhpSpreadsheet/Writer/Xlsx.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,9 @@ public function save($filename, int $flags = 0): void
431431
foreach ($unparsedLoadedData['sheets'][$sheetCodeName]['Drawings'] as $relId => $drawingXml) {
432432
$drawingFile = array_search($relId, $unparsedLoadedData['sheets'][$sheetCodeName]['drawingOriginalIds']);
433433
if ($drawingFile !== false) {
434-
$drawingFile = ltrim($drawingFile, '.');
435-
$zipContent['xl' . $drawingFile] = $drawingXml;
434+
//$drawingFile = ltrim($drawingFile, '.');
435+
//$zipContent['xl' . $drawingFile] = $drawingXml;
436+
$zipContent['xl/drawings/drawing' . ($i + 1) . '.xml'] = $drawingXml;
436437
}
437438
}
438439
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;
4+
5+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as Reader;
6+
use PhpOffice\PhpSpreadsheet\Shared\File;
7+
use PhpOffice\PhpSpreadsheet\Worksheet\BaseDrawing;
8+
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
9+
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as Writer;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class Unparsed2396Test extends TestCase
13+
{
14+
/** @var string */
15+
private $filename = '';
16+
17+
protected function tearDown(): void
18+
{
19+
if ($this->filename !== '') {
20+
unlink($this->filename);
21+
$this->filename = '';
22+
}
23+
}
24+
25+
private function getContents(?BaseDrawing $drawing): string
26+
{
27+
$contents = '';
28+
if ($drawing instanceof Drawing) {
29+
$contents = (string) file_get_contents($drawing->getPath());
30+
} else {
31+
self::fail('Unexpected null or baseDrawing which is not Drawing');
32+
}
33+
self::assertNotSame('', $contents);
34+
35+
return $contents;
36+
}
37+
38+
// Don't drop image as in issue 2396.
39+
public function testUnparsed2396(): void
40+
{
41+
$sampleFilename = 'tests/data/Writer/XLSX/issue.2396.xlsx';
42+
$reader = new Reader();
43+
$excel = $reader->load($sampleFilename);
44+
$outputFilename = $this->filename = File::temporaryFilename();
45+
$writer = new Writer($excel);
46+
$writer->save($outputFilename);
47+
//$spreadsheet = $this->writeAndReload($excel, 'Xlsx');
48+
$excel->disconnectWorksheets();
49+
$reader = new Reader();
50+
$spreadsheet = $reader->load($outputFilename);
51+
$sheet = $spreadsheet->getSheet(0);
52+
$drawing1 = $sheet->getDrawingCollection();
53+
self::assertCount(1, $drawing1);
54+
$hash = $this->getContents($drawing1[0]);
55+
56+
$sheet = $spreadsheet->getSheet(1);
57+
$drawings = $sheet->getDrawingCollection();
58+
self::assertCount(1, $drawings);
59+
self::assertSame($hash, $this->getContents($drawings[0]));
60+
61+
$sheet = $spreadsheet->getSheet(2);
62+
$drawings = $sheet->getDrawingCollection();
63+
self::assertCount(0, $drawings);
64+
//self::assertSame($hash, md5(file_get_contents($drawings[0]->getPath())));
65+
66+
$sheet = $spreadsheet->getSheet(3);
67+
$drawings = $sheet->getDrawingCollection();
68+
self::assertCount(1, $drawings);
69+
self::assertSame($hash, $this->getContents($drawings[0]));
70+
71+
$sheet = $spreadsheet->getSheet(4);
72+
$drawings = $sheet->getDrawingCollection();
73+
self::assertCount(0, $drawings);
74+
//self::assertSame($hash, md5(file_get_contents($drawings[0]->getPath())));
75+
76+
// The next 2 sheets have 'legacyDrawing', button, and listbox.
77+
// If support is added for those, these tests may need adjustment.
78+
$sheet = $spreadsheet->getSheet(5);
79+
$drawings = $sheet->getDrawingCollection();
80+
self::assertCount(0, $drawings);
81+
//self::assertSame($hash, md5(file_get_contents($drawings[0]->getPath())));
82+
83+
$sheet = $spreadsheet->getSheet(6);
84+
$drawings = $sheet->getDrawingCollection();
85+
self::assertCount(1, $drawings);
86+
self::assertSame($hash, $this->getContents($drawings[0]));
87+
88+
$spreadsheet->disconnectWorksheets();
89+
}
90+
}
66.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)