{"record":{"id":"7678ff614c8b2047","repo":"PHPOffice/PhpSpreadsheet","slug":"sheet-does-not-have-a-chart-named-chartname","errorCode":null,"errorMessage":"Sheet does not have a chart named $chartName.","messagePattern":"Sheet does not have a chart named \\$chartName\\.","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Worksheet/Worksheet.php","lineNumber":657,"sourceCode":"    public function getChartByName(string $chartName)\n    {\n        foreach ($this->chartCollection as $index => $chart) {\n            if ($chart->getName() == $chartName) {\n                return $chart;\n            }\n        }\n\n        return false;\n    }\n\n    public function getChartByNameOrThrow(string $chartName): Chart\n    {\n        $chart = $this->getChartByName($chartName);\n        if ($chart !== false) {\n            return $chart;\n        }\n\n        throw new Exception(\"Sheet does not have a chart named $chartName.\");\n    }\n\n    /**\n     * Refresh column dimensions.\n     *\n     * @return $this\n     */\n    public function refreshColumnDimensions(): static\n    {\n        $newColumnDimensions = [];\n        foreach ($this->getColumnDimensions() as $objColumnDimension) {\n            $newColumnDimensions[$objColumnDimension->getColumnIndex()] = $objColumnDimension;\n        }\n\n        $this->columnDimensions = $newColumnDimensions;\n\n        return $this;\n    }","sourceCodeStart":639,"sourceCodeEnd":675,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Worksheet/Worksheet.php#L639-L675","documentation":"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'.","triggerScenarios":"$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.","commonSituations":"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.","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"],"exampleFix":"// before\n$chart = $sheet->getChartByNameOrThrow('SalesChart'); // throws if absent\n\n// after\n$chart = $sheet->getChartByName('SalesChart');\nif ($chart === false) {\n    // log / render placeholder / search other sheets\n}","handlingStrategy":"type-guard","validationCode":"$chart = $sheet->getChartByName('SalesChart');\nif ($chart === false) {\n    // enumerate to find the real name\n    foreach ($sheet->getChartCollection() as $c) {\n        // inspect $c->getName()\n    }\n    return;\n}","typeGuard":"/** @param mixed $chart Chart|false from getChartByName() */\nfunction chartFound($chart): bool\n{\n    return $chart !== false;\n}","tryCatchPattern":"try {\n    $chart = $sheet->getChartByNameOrThrow($name);\n} catch (\\PhpOffice\\PhpSpreadsheet\\Exception $e) {\n    if (str_contains($e->getMessage(), 'does not have a chart named')) {\n        // fall back to positional access: $sheet->getChartByIndex(0)\n    } else {\n        throw $e;\n    }\n}","preventionTips":["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"],"tags":["phpspreadsheet","worksheet","chart","not-found","lookup"],"backgroundTag":"resource-not-found","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}