Skip to content

Commit

Permalink
Fix chart data references on sheet title update
Browse files Browse the repository at this point in the history
Chart with sheet reference was not updated on sheet title renaming.

Fix PHPOffice#744
  • Loading branch information
guillaume-ro-fr committed Nov 6, 2018
1 parent 7051a97 commit 4535819
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/PhpSpreadsheet/ReferenceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class ReferenceHelper
Expand Down Expand Up @@ -819,6 +821,39 @@ public function updateNamedFormulas(Spreadsheet $spreadsheet, $oldName = '', $ne
}
}
}

foreach ($sheet->getChartCollection() as $chart) {
/** @var DataSeries $plotGroup */
foreach ($chart->getPlotArea()->getPlotGroup() as $plotGroup) {
/** @var DataSeriesValues $plotCategory */
foreach ($plotGroup->getPlotCategories() as $plotCategory) {
$dataSource = $plotCategory->getDataSource();
if (strpos($dataSource, $oldName) !== false) {
$dataSource = str_replace("'" . $oldName . "'!", "'" . $newName . "'!", $dataSource);
$dataSource = str_replace($oldName . '!', $newName . '!', $dataSource);
$plotCategory->setDataSource($dataSource);
}
}
/** @var DataSeriesValues $plotLabel */
foreach ($plotGroup->getPlotLabels() as $plotLabel) {
$dataSource = $plotLabel->getDataSource();
if (strpos($dataSource, $oldName) !== false) {
$dataSource = str_replace("'" . $oldName . "'!", "'" . $newName . "'!", $dataSource);
$dataSource = str_replace($oldName . '!', $newName . '!', $dataSource);
$plotLabel->setDataSource($dataSource);
}
}
/** @var DataSeriesValues $plotValue */
foreach ($plotGroup->getPlotValues() as $plotValue) {
$dataSource = $plotValue->getDataSource();
if (strpos($dataSource, $oldName) !== false) {
$dataSource = str_replace("'" . $oldName . "'!", "'" . $newName . "'!", $dataSource);
$dataSource = str_replace($oldName . '!', $newName . '!', $dataSource);
$plotValue->setDataSource($dataSource);
}
}
}
}
}
}

Expand Down

0 comments on commit 4535819

Please sign in to comment.