Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Name Clashes Between Parsed and Unparsed Drawings #2423

Merged
merged 5 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions samples/Reader/22_Reader_issue1767.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use PhpOffice\PhpSpreadsheet\IOFactory;

require __DIR__ . '/../Header.php';

$helper->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');
Binary file added samples/Reader/sampleData/issue.1767.xlsx
Binary file not shown.
5 changes: 3 additions & 2 deletions src/PhpSpreadsheet/Writer/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
90 changes: 90 additions & 0 deletions tests/PhpSpreadsheetTests/Writer/Xlsx/Unparsed2396Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;

use PhpOffice\PhpSpreadsheet\Reader\Xlsx as Reader;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Worksheet\BaseDrawing;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as Writer;
use PHPUnit\Framework\TestCase;

class Unparsed2396Test extends TestCase
{
/** @var string */
private $filename = '';

protected function tearDown(): void
{
if ($this->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();
}
}
Binary file added tests/data/Writer/XLSX/issue.2396.xlsx
Binary file not shown.