PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception
Sheet does not have a chart named $chartName.
Error message
Sheet does not have a chart named $chartName.
What it means
Thrown by Worksheet::getChartByNameOrThrow() when getChartByName() returns false, i.e. no chart in this worksheet's chart collection carries the exact requested name. Chart lookup is per-worksheet and name-exact, so a chart living on another sheet or named differently (case, spaces) is 'not found'.
Source
Thrown at src/PhpSpreadsheet/Worksheet/Worksheet.php:657
public function getChartByName(string $chartName)
{
foreach ($this->chartCollection as $index => $chart) {
if ($chart->getName() == $chartName) {
return $chart;
}
}
return false;
}
public function getChartByNameOrThrow(string $chartName): Chart
{
$chart = $this->getChartByName($chartName);
if ($chart !== false) {
return $chart;
}
throw new Exception("Sheet does not have a chart named $chartName.");
}
/**
* Refresh column dimensions.
*
* @return $this
*/
public function refreshColumnDimensions(): static
{
$newColumnDimensions = [];
foreach ($this->getColumnDimensions() as $objColumnDimension) {
$newColumnDimensions[$objColumnDimension->getColumnIndex()] = $objColumnDimension;
}
$this->columnDimensions = $newColumnDimensions;
return $this;
}View on GitHub (pinned to 65b080eef4)
Solutions
- Enumerate charts first: foreach ($sheet->getChartCollection() as $chart) { ... $chart->getName() } and match dynamically
- Use the non-throwing $sheet->getChartByName($name) and check !== false before using the result
- Verify you are on the right sheet: search all sheets' collections for the name
Example fix
// before
$chart = $sheet->getChartByNameOrThrow('SalesChart'); // throws if absent
// after
$chart = $sheet->getChartByName('SalesChart');
if ($chart === false) {
// log / render placeholder / search other sheets
} Defensive patterns
Strategy: type-guard
Validate before calling
$chart = $sheet->getChartByName('SalesChart');
if ($chart === false) {
// enumerate to find the real name
foreach ($sheet->getChartCollection() as $c) {
// inspect $c->getName()
}
return;
} Type guard
/** @param mixed $chart Chart|false from getChartByName() */
function chartFound($chart): bool
{
return $chart !== false;
} Try / catch
try {
$chart = $sheet->getChartByNameOrThrow($name);
} catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
if (str_contains($e->getMessage(), 'does not have a chart named')) {
// fall back to positional access: $sheet->getChartByIndex(0)
} else {
throw $e;
}
} Prevention
- Prefer getChartByName() + false check in tolerant code paths
- Discover chart names dynamically from getChartCollection() instead of hardcoding
- Remember chart lookup is per-sheet — resolve the right worksheet first
When it happens
Trigger: $sheet->getChartByNameOrThrow('Chart 5') when the sheet has no charts or the chart is named 'chart 5'/'Chart5'; calling it on the active sheet when the chart actually sits on another sheet; after renaming charts in the source xlsx.
Common situations: Hardcoding chart names that change between template versions; reading user-uploaded files whose chart names differ; assuming chart names are workbook-global when they are per-sheet.
Related errors
- #N/A
- #VALUE!
- Cannot update when cell is not bound to a worksheet
- Cannot get column when cell is not bound to a worksheet
- Cannot get row when cell is not bound to a worksheet
AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17).
Data as JSON: /api/errors/7678ff614c8b2047.
Report an issue: GitHub.