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

Chart title loses reference to cell #3797

Closed
2 of 8 tasks
roland-jungwirth opened this issue Nov 23, 2023 · 5 comments · Fixed by #3800
Closed
2 of 8 tasks

Chart title loses reference to cell #3797

roland-jungwirth opened this issue Nov 23, 2023 · 5 comments · Fixed by #3800
Labels

Comments

@roland-jungwirth
Copy link

roland-jungwirth commented Nov 23, 2023

This is:

- [x] a bug report

What is the expected behavior?

If I have a graph title that relates to a cell, this should stay as is. I.e. the graph title's value is "=A1", deriving the value from Cell A1.

What is the current behavior?

When the graph has gone through the reader and writer, the output does have the correct data, i.e. the value of A1, but it is not a reference to A1 anymore.

What are the steps to reproduce?

<?php

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

$template_file = 'test-rotated_xlabel.xlsx';
$filename = 'test_' . date( 'Y-m-d_H-i-s' ) . '.xlsx';

// Load the template file
$reader = IOFactory::createReader('Xlsx');
$reader->setIncludeCharts(TRUE);

$spreadsheet = $reader->load($template_file);

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

// 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' );
exit;

Here is the sample Excel file that I use:
test-rotated_xlabel.xlsx

What features do you think are causing the issue

  • Reader
  • Writer
  • Styles
  • Data Validations
  • Formula Calculations
  • Charts
  • AutoFilter
  • Form Elements

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

Which versions of PhpSpreadsheet and PHP are affected?

PHP 8.1

@oleibman
Copy link
Collaborator

Thank you for the sample file. PhpSpreadsheet does not support using a formula for a title. I will investigate whether it can be supported.

@roland-jungwirth
Copy link
Author

Excel doesn't either - that's why I have the formula in another cell and reference that. Excel does support that.

@oleibman
Copy link
Collaborator

Excel stores the cell references as if they were formulas. I agree that, at least at present, Excel restricts the formulas to references to a single cell (and sheet name must be part of the reference). I'm not sure that's a safe long-term assumption (but it probably is).

Your example sheet pointed out something else missing from PhpSpreadsheet. Your title font specifies all-caps, which is not an attribute that you can apply to a font for a cell. I'll have to see if it can be supported.

@roland-jungwirth
Copy link
Author

So is there - currently - a way of setting a chart's title using PhpSpreadsheet?

@oleibman
Copy link
Collaborator

Yes, the chart has a Title object, which has a caption property. Most of the 33* members in samples/Chart do this. What you can't do yet is make the title dynamic, which is the issue you raised.

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Nov 28, 2023
Fix PHPOffice#3797. Excel allows a Chart Title to be a formula, albeit a very rigidly limited one. It can only be a reference to a single cell, and the worksheet name must be specified, and the column and row must be absolute. Methods are added to Chart/Title to accommodate this (and styling for it). This will be handled for input/output for Xlsx, and for output for Html.

The sample file which was submitted with this issue demonstrated that something else was missing. When setting the font for a chart title in Excel, you can specify all-caps or small-caps, options not available for most cell formatting. These are now added.

The sample file also fell into the category of spreadsheets which lose one or more charts when converted to Html. I have redone the "extend rows and charts" logic in Html Writer. It is now clearer (I hope) and more efficient, and hopefully this problem will not arise again.
oleibman added a commit that referenced this issue Nov 30, 2023
* Chart Dynamic Title and Special Font Properties

Fix #3797. Excel allows a Chart Title to be a formula, albeit a very rigidly limited one. It can only be a reference to a single cell, and the worksheet name must be specified, and the column and row must be absolute. Methods are added to Chart/Title to accommodate this (and styling for it). This will be handled for input/output for Xlsx, and for output for Html.

The sample file which was submitted with this issue demonstrated that something else was missing. When setting the font for a chart title in Excel, you can specify all-caps or small-caps, options not available for most cell formatting. These are now added.

The sample file also fell into the category of spreadsheets which lose one or more charts when converted to Html. I have redone the "extend rows and charts" logic in Html Writer. It is now clearer (I hope) and more efficient, and hopefully this problem will not arise again.

* Scrutinizer 50/50

One false positive, one correct "unused parameter".
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.

2 participants