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

Saving IOFactory::WRITER_XLSX does not work for an Amazon Template for their CPF Program (Climate Pledge Friendly) #3679

Closed
7 tasks
xonu opened this issue Aug 19, 2023 · 2 comments · Fixed by #3681

Comments

@xonu
Copy link

xonu commented Aug 19, 2023

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?

The file.xlsx saved successfully.

What is the current behavior?

Saving file does not work:

Fatal error:  Uncaught Error: Object of class PhpOffice\PhpSpreadsheet\Calculation\Engine\Operands\StructuredReference could not be converted to string in /app/excel/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Calculation/Calculation.php:4716
Stack trace:
#0 /app/excel/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Calculation/Calculation.php(3761): PhpOffice\PhpSpreadsheet\Calculation\Calculation->processTokenStack()
#1 /app/excel/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Calculation/Calculation.php(3535): PhpOffice\PhpSpreadsheet\Calculation\Calculation->_calculateFormulaValue()
#2 /app/excel/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/Cell.php(380): PhpOffice\PhpSpreadsheet\Calculation\Calculation->calculateCellValue()
#3 /app/excel/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php(1274): PhpOffice\PhpSpreadsheet\Cell\Cell->getCalculatedValue()
#4 /app/excel/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php(1343): PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet->writeCellFormula()
#5 /app/excel/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php(1195): PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet->writeCell()
#6 /app/excel/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php(87): PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet->writeSheetData()
#7 /app/excel/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx.php(393): PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet->writeWorksheet()
#8 /app/excel/index.php(23): PhpOffice\PhpSpreadsheet\Writer\Xlsx->save()
#9 {main}
  thrown in /app/excel/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Calculation/Calculation.php on line 4716

What are the steps to reproduce?

Nothing very special, just load the file and save it (without even changing anything) like that:

<php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\IOFactory;

$filename = 'Amazon.CPF.empty';
$filePath = "input/$filename.xlsx";
$spreadsheet = IOFactory::load($filePath);

$xlsWriter = IOFactory::createWriter($spreadsheet, IOFactory::WRITER_XLSX);
$xlsWriter->save($filePathOutput = "output/$filename.xlsx");

If this is an issue with reading and writing a specific spreadsheet file:
Amazon CPF empty.xlsx

What features do you think are causing the issue

  • Reader

  • [x ] Writer

  • Styles

  • Data Validations

  • Formula Calculations

  • Charts

  • AutoFilter

  • Form Elements

Does an issue affect all spreadsheet file formats? If not, which formats are affected?

XLSX IOFactory::WRITER_XLSX

Which versions of PhpSpreadsheet and PHP are affected?

1.29.0 (currently latest)

@oleibman
Copy link
Collaborator

The reported problem is fixed by PR #3659, which has just been merged. I am leaving the issue open because loading and saving the spreadsheet isn't quite perfect, in particular some dropdowns are lost.

@oleibman
Copy link
Collaborator

Interesting case. Data Validations and Conditional Styles can each be supplied in the Xml in either "external" or "internal" formats. The code for each to handle "external" assumes that each is the only "external" item on the worksheet in the Xml, but some of the worksheets in the sample spreadsheet provide both as "external" on some sheets. I will work on a solution.

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Aug 20, 2023
Fix PHPOffice#3679. That issue was opened for a problem which was already solved by PR PHPOffice#3659, however there were additional problems with the spreadsheet.

The main problem is that Data Validations and Conditional Styles can each be supplied in the Xml in either "external" or "internal" formats. The code for each to handle "external" assumes that each is the only "external" item on the worksheet in the Xml, but some of the worksheets in the sample spreadsheet provide both as "external" on some sheets. The code to fix this is verified against the supplied sample, however no formal test has been added for it. The sample is much too large and complicated to be added to the test suite - it takes several minutes to read, and even longer to write (`setPreCalculateFormulas(false)` is highly recommended). I will leave this ticket open for a few days to see if I can hand-craft a suitable test case, but I am not hopeful.

A second problem is that something in the Xlsx Reader `$xmlSheetNS->sheetData->row` loop breaks the selected cell for the worksheet. This is easily fixed and verified by eye (and with the supplied sample), but, again, no explicit test case is added.

A third problem is that drawings which are part of the supplied sample use `srcRect` tags in the Xml to effectively produce a cropped version of the image. This tag has hitherto been ignored. It is now supported in Xlsx Reader, Xlsx Writer, and Worksheet/BaseDrawing object. This is again verified with the supplied sample; unlike the other parts, it was easy to add a new formal test case for this part of the fix.
oleibman added a commit that referenced this issue Aug 27, 2023
* Fix Several Problems in a Very Complicated Spreadsheet

Fix #3679. That issue was opened for a problem which was already solved by PR #3659, however there were additional problems with the spreadsheet.

The main problem is that Data Validations and Conditional Styles can each be supplied in the Xml in either "external" or "internal" formats. The code for each to handle "external" assumes that each is the only "external" item on the worksheet in the Xml, but some of the worksheets in the sample spreadsheet provide both as "external" on some sheets. The code to fix this is verified against the supplied sample, however no formal test has been added for it. The sample is much too large and complicated to be added to the test suite - it takes several minutes to read, and even longer to write (`setPreCalculateFormulas(false)` is highly recommended). I will leave this ticket open for a few days to see if I can hand-craft a suitable test case, but I am not hopeful.

A second problem is that something in the Xlsx Reader `$xmlSheetNS->sheetData->row` loop breaks the selected cell for the worksheet. This is easily fixed and verified by eye (and with the supplied sample), but, again, no explicit test case is added.

A third problem is that drawings which are part of the supplied sample use `srcRect` tags in the Xml to effectively produce a cropped version of the image. This tag has hitherto been ignored. It is now supported in Xlsx Reader, Xlsx Writer, and Worksheet/BaseDrawing object. This is again verified with the supplied sample; unlike the other parts, it was easy to add a new formal test case for this part of the fix.

* Scrutinizer False Positives
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants