From 3edcc7fe6f694f7e9bb157c8a83013c7a696f6f8 Mon Sep 17 00:00:00 2001 From: Guillaume RODRIGUEZ Date: Mon, 29 Oct 2018 09:50:31 +0100 Subject: [PATCH] Fix chart data references on sheet title update Chart with sheet reference was not updated on sheet title renaming. Fix #744 --- src/PhpSpreadsheet/ReferenceHelper.php | 35 ++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/PhpSpreadsheet/ReferenceHelper.php b/src/PhpSpreadsheet/ReferenceHelper.php index 0b0f6dd3c6..a8083a8ef5 100644 --- a/src/PhpSpreadsheet/ReferenceHelper.php +++ b/src/PhpSpreadsheet/ReferenceHelper.php @@ -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 @@ -807,6 +809,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); + } + } + } + } } }