Skip to content

Limited Support for Chart Titles as Formulas #2971

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

Merged
merged 2 commits into from
Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/PhpSpreadsheet/Reader/Xlsx/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,20 @@ private function chartTitle(SimpleXMLElement $titleDetails): Title
foreach ($titleDetails as $titleDetailKey => $chartDetail) {
switch ($titleDetailKey) {
case 'tx':
$titleDetails = $chartDetail->rich->children($this->aNamespace);
foreach ($titleDetails as $titleKey => $titleDetail) {
switch ($titleKey) {
case 'p':
$titleDetailPart = $titleDetail->children($this->aNamespace);
$caption[] = $this->parseRichText($titleDetailPart);
if (isset($chartDetail->rich)) {
$titleDetails = $chartDetail->rich->children($this->aNamespace);
foreach ($titleDetails as $titleKey => $titleDetail) {
switch ($titleKey) {
case 'p':
$titleDetailPart = $titleDetail->children($this->aNamespace);
$caption[] = $this->parseRichText($titleDetailPart);
}
}
} elseif (isset($chartDetail->strRef->strCache)) {
foreach ($chartDetail->strRef->strCache->pt as $pt) {
if (isset($pt->v)) {
$caption[] = (string) $pt->v;
}
}
}

Expand Down
42 changes: 42 additions & 0 deletions tests/PhpSpreadsheetTests/Chart/Issue2965Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Chart;

use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
use PHPUnit\Framework\TestCase;

class Issue2965Test extends TestCase
{
private const DIRECTORY = 'tests' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'Reader' . DIRECTORY_SEPARATOR . 'XLSX' . DIRECTORY_SEPARATOR;

public function testPreliminaries(): void
{
$file = 'zip://';
$file .= self::DIRECTORY . 'issue.2965.xlsx';
$file .= '#xl/charts/chart1.xml';
$data = file_get_contents($file);
// confirm that file contains expected namespaced xml tag
if ($data === false) {
self::fail('Unable to read file');
} else {
self::assertStringContainsString('<c:title><c:tx><c:strRef><c:f>Sheet1!$A$1</c:f><c:strCache><c:ptCount val="1"/><c:pt idx="0"><c:v>NewTitle</c:v></c:pt></c:strCache></c:strRef></c:tx>', $data);
}
}

public function testChartTitleFormula(): void
{
$reader = new XlsxReader();
$reader->setIncludeCharts(true);
$spreadsheet = $reader->load(self::DIRECTORY . 'issue.2965.xlsx');
$worksheet = $spreadsheet->getActiveSheet();
$charts = $worksheet->getChartCollection();
self::assertCount(1, $charts);
$originalChart1 = $charts[0];
self::assertNotNull($originalChart1);
$originalTitle1 = $originalChart1->getTitle();
self::assertNotNull($originalTitle1);
self::assertSame('NewTitle', $originalTitle1->getCaptionText());

$spreadsheet->disconnectWorksheets();
}
}
Binary file added tests/data/Reader/XLSX/issue.2965.xlsx
Binary file not shown.