PHPOffice / PhpSpreadsheet

A pure PHP library for reading and writing spreadsheet files
https://phpspreadsheet.readthedocs.io
MIT License
13.36k stars 3.47k forks source link

Excel from template didn't copy charts. Random source data. #3478

Open rumcais opened 1 year ago

rumcais commented 1 year ago

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?

Should copy Charts if there exists.

What is the current behavior?

Error during generation if the graph is based on random rows.: PhpOffice\PhpSpreadsheet\Calculation\Exception Formula Error: Unexpected ,

What are the steps to reproduce?

Please provide a Minimal, Complete, and Verifiable example of code that exhibits the issue without relying on an external Excel file or a web server:

<?php

require __DIR__ . '/vendor/autoload.php';

        $inputFileType = 'Xlsx';
        $inputFileName = 'Graph.xlsx';
        $reader = IOFactory::createReader($inputFileType);
        $reader->setIncludeCharts(true);
        $spreadsheet = $reader->load($inputFileName);
       $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->setIncludeCharts(true);
        $writer->save('output.xslx');

If this is an issue with reading a specific spreadsheet file, then it may be appropriate to provide a sample file that demonstrates the problem; but please keep it as small as possible, and sanitize any confidential information before uploading.

This template works https://ufile.io/3pkwv0xz This template is not working https://ufile.io/gktlbr4j

MarkBaker commented 1 year ago

The problem is that TIL about the existence of MS Excel's SERIES() function.

At the moment, it simply doesn't exist in PhpSpreadsheet; but the graph uses it in a formula, and PhpSpreadsheet tries to evaluate that formula.

MarkBaker commented 1 year ago

As a temporary solution (until we can implement SERIES() and integrate it with Charts), you can disable the Calculation Engine before saving the file:

$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->setIncludeCharts(true);
$writer->setPreCalculateFormulas(false);
$writer->save('output.xslx');
rumcais commented 1 year ago

Super. Thank you.