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

Wrong Ids relations when sheet clone #795

Closed
cia1 opened this issue Dec 1, 2018 · 2 comments · Fixed by #1462
Closed

Wrong Ids relations when sheet clone #795

cia1 opened this issue Dec 1, 2018 · 2 comments · Fixed by #1462
Labels

Comments

@cia1
Copy link

cia1 commented Dec 1, 2018

This is:

- [x] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

Correct saved xlsx file.

What is the current behavior?

File is broken.

What are the steps to reproduce?

  • I have xlsx document, contains one empty page with only one image (example file attached);
  • load xlsx;
  • clone first sheet and add to spreadsheet;
  • save to a file.
<?php
$template = IOFactory::load('some-src.xlsx');
$sheet = $template->getSheet(0)->copy();
$sheet->setTitle('Some sheet 2');
$template->addSheet($sheet);
$writer = new Xlsx($template);
$writer->save('some-dst.xlsx');

So... I unzip some-dst.xlsx and see file "xl/worksheets/_rels/sheet2.xml.rels":

There two relations have some Id (rId1).
Please, looks at the PhpOffice\PhpSpreadsheet\Writer\Xlsx\Rels::writeWorksheetRelationships(). I think that array $drawingOriginalIds have to contains items for new clonned page. I think there need to update array $drawingOriginalIds when any sheet will be cloned.

To fix this a use this way, but there is a very bad solution...

<?php
$template = IOFactory::load('some-src.xlsx');
$sheet = $template->getSheet(0)->copy();
$sheet->setTitle('Some sheet 2');
$template->addSheet($sheet);

$raw = $template->getUnparsedLoadedData();
$drawingOriginalIds = $raw['sheets'][$sheet->getCodeName()]['drawingOriginalIds'];
$cnt = count($drawingOriginalIds);
$key = '../drawings/drawing' . $cnt . '.xml';
$drawingOriginalIds['../drawings/drawing' . ($cnt + 1) . '.xml'] = $drawingOriginalIds[$key];
$raw['sheets'][$sheet->getCodeName()]['drawingOriginalIds'] = $drawingOriginalIds;
$template->setUnparsedLoadedData($raw);

$writer = new Xlsx($template);
$writer->save('some-dst.xlsx');

Which versions of PhpSpreadsheet and PHP are affected?

PHP 7.1
Spreadsheet 1.5.2

@cia1
Copy link
Author

cia1 commented Dec 2, 2018

sheet2.xml.rels (cutted by editor):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> ... <Relationship Id="rId1" Type=".../drawing" Target="../drawings/drawing2.xml"/> <Relationship Id="rId1" Type=".../printerSettings" Target="../printerSettings/printerSettings1.bin"/>

@stale
Copy link

stale bot commented Feb 2, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this is still an issue for you, please try to help by debugging it further and sharing your results.
Thank you for your contributions.

@stale stale bot added the stale label Feb 2, 2019
@stale stale bot closed this as completed Feb 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

1 participant