diff --git a/src/PhpSpreadsheet/ReferenceHelper.php b/src/PhpSpreadsheet/ReferenceHelper.php index 54bc182a8e..ef3e4f1100 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 @@ -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); + } + } + } + } } }