Fix saving XLSX with drawings (#1462)
* Fix incorrect behaviour when saving XLSX file with drawings
This commit is contained in:
parent
6c23de78d8
commit
3446bb0ef7
|
@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
|
|||
- Fix RATE, PRICE, XIRR, and XNPV Functions [#1456](https://github.com/PHPOffice/PhpSpreadsheet/pull/1456)
|
||||
- Save Excel 2010+ functions properly in XLSX [#1461](https://github.com/PHPOffice/PhpSpreadsheet/pull/1461)
|
||||
- Several improvements in HTML writer [#1464](https://github.com/PHPOffice/PhpSpreadsheet/pull/1464)
|
||||
- Fix incorrect behaviour when saving XLSX file with drawings [#1462](https://github.com/PHPOffice/PhpSpreadsheet/pull/1462),
|
||||
- Fix Crash while trying setting a cell the value "123456\n" [#1476](https://github.com/PHPOffice/PhpSpreadsheet/pull/1481)
|
||||
|
||||
### Changed
|
||||
|
|
|
@ -183,7 +183,6 @@ class Rels extends WriterPart
|
|||
$objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
|
||||
|
||||
// Write drawing relationships?
|
||||
$d = 0;
|
||||
$drawingOriginalIds = [];
|
||||
$unparsedLoadedData = $pWorksheet->getParent()->getUnparsedLoadedData();
|
||||
if (isset($unparsedLoadedData['sheets'][$pWorksheet->getCodeName()]['drawingOriginalIds'])) {
|
||||
|
@ -197,13 +196,19 @@ class Rels extends WriterPart
|
|||
}
|
||||
|
||||
if (($pWorksheet->getDrawingCollection()->count() > 0) || (count($charts) > 0) || $drawingOriginalIds) {
|
||||
$relPath = '../drawings/drawing' . $pWorksheetId . '.xml';
|
||||
$rId = ++$d;
|
||||
$rId = 1;
|
||||
|
||||
// Use original $relPath to get original $rId.
|
||||
// Take first. In future can be overwritten.
|
||||
// (! synchronize with \PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet::writeDrawings)
|
||||
reset($drawingOriginalIds);
|
||||
$relPath = key($drawingOriginalIds);
|
||||
if (isset($drawingOriginalIds[$relPath])) {
|
||||
$rId = (int) (substr($drawingOriginalIds[$relPath], 3));
|
||||
}
|
||||
|
||||
// Generate new $relPath to write drawing relationship
|
||||
$relPath = '../drawings/drawing' . $pWorksheetId . '.xml';
|
||||
$this->writeRelationship(
|
||||
$objWriter,
|
||||
$rId,
|
||||
|
|
|
@ -1215,6 +1215,7 @@ class Worksheet extends WriterPart
|
|||
if (isset($unparsedLoadedData['sheets'][$pSheet->getCodeName()]['drawingOriginalIds'])) {
|
||||
$drawingOriginalIds = $unparsedLoadedData['sheets'][$pSheet->getCodeName()]['drawingOriginalIds'];
|
||||
// take first. In future can be overriten
|
||||
// (! synchronize with \PhpOffice\PhpSpreadsheet\Writer\Xlsx\Rels::writeWorksheetRelationships)
|
||||
$rId = reset($drawingOriginalIds);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
|
||||
use PhpOffice\PhpSpreadsheet\Settings;
|
||||
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
|
||||
|
||||
class DrawingsTest extends AbstractFunctional
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $prevValue;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->prevValue = Settings::getLibXmlLoaderOptions();
|
||||
|
||||
// Disable validating XML with the DTD
|
||||
Settings::setLibXmlLoaderOptions($this->prevValue & ~LIBXML_DTDVALID & ~LIBXML_DTDATTR & ~LIBXML_DTDLOAD);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Settings::setLibXmlLoaderOptions($this->prevValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test save and load XLSX file with drawing on 2nd worksheet.
|
||||
*/
|
||||
public function testSaveLoadWithDrawingOn2ndWorksheet(): void
|
||||
{
|
||||
// Read spreadsheet from file
|
||||
$inputFilename = 'tests/data/Writer/XLSX/drawing_on_2nd_page.xlsx';
|
||||
$reader = new Xlsx();
|
||||
$spreadsheet = $reader->load($inputFilename);
|
||||
|
||||
// Save spreadsheet to file and read it back
|
||||
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx');
|
||||
|
||||
// Fake assert. The only thing we need is to ensure the file is loaded without exception
|
||||
self::assertNotNull($reloadedSpreadsheet);
|
||||
}
|
||||
}
|
Binary file not shown.
Loading…
Reference in New Issue