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

Chart Position Inconsistent with Original Spreadsheet #3912

Open roland-jungwirth opened 9 months ago

roland-jungwirth commented 9 months 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?

If I read a spreadsheet with a chart, I expect the chart to be in the same position when I output the spreadsheet.

What is the current behavior?

The chart is in B24, but - after I have read and output it - it is in D13.

Additional background

I was trying to insert some rows and noticed that the chart was not being moved down, although this is the standard behaviour for Excel (reproduce: open the attached test.xlsx, mark lines 20-22, right click, click "Insert"). During my testing this issue came up.

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';

$reader = IOFactory::createReader( 'Xlsx' );
$reader->setIncludeCharts( TRUE );

$spreadsheet = $reader->load( '/test.xlsx' );

// Set the headers to force a download of the Excel file
header( 'Content-Type: application/vnd.ms-excel' );
header( 'Content-Disposition: attachment; filename="test-output.xlsx"' );
header( 'Cache-Control: max-age=0' );

//$input_worksheet = $spreadsheet->getSheetByName( 'Sheet1' );
//$input_worksheet->insertNewRowBefore( 21, 10 );

// Create a writer and send the Excel file to the browser
$writer = new Xlsx( $spreadsheet );

ob_end_clean();
$writer->setIncludeCharts( TRUE );
$writer->save( 'php://output' );

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.

What features do you think are causing the issue

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

Only tested with xlsx.

Which versions of PhpSpreadsheet and PHP are affected?

PHP 8.1, PhpSpreadsheet: whatever this version is: https://github.com/PHPOffice/PhpSpreadsheet/issues/3767

test.xlsx

oleibman commented 9 months ago

Unable to duplicate. When I load and save the spreadsheet to a new file, the chart is in B24, as expected. I am, admittedly, running from the command line rather than a web server, but I do not see why that should matter.

You do not seem to be specifying includeCharts on the writer, but that would cause the chart to be omitted from the output file, not moved to a different location.

roland-jungwirth commented 9 months ago

I omitted includeCharts by accident - it definitely shows in the output. It might be due to a different version of phpspreadsheet?