{"record":{"id":"1591fc178e98286e","repo":"PHPOffice/PhpSpreadsheet","slug":"workbook-does-not-contain-sheet-worksheetname","errorCode":null,"errorMessage":"Workbook does not contain sheet:{$worksheetName}","messagePattern":"Workbook does not contain sheet:(.+?)","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Spreadsheet.php","lineNumber":843,"sourceCode":"        $this->activeSheetIndex = $worksheetIndex;\n\n        return $this->getActiveSheet();\n    }\n\n    /**\n     * Set active sheet index by name.\n     *\n     * @param string $worksheetName Sheet title\n     */\n    public function setActiveSheetIndexByName(string $worksheetName): Worksheet\n    {\n        if (($worksheet = $this->getSheetByName($worksheetName)) instanceof Worksheet) {\n            $this->setActiveSheetIndex($this->getIndex($worksheet));\n\n            return $worksheet;\n        }\n\n        throw new Exception('Workbook does not contain sheet:' . $worksheetName);\n    }\n\n    /**\n     * Get sheet names.\n     *\n     * @return string[]\n     */\n    public function getSheetNames(): array\n    {\n        $returnValue = [];\n        $worksheetCount = $this->getSheetCount();\n        for ($i = 0; $i < $worksheetCount; ++$i) {\n            $returnValue[] = $this->getSheet($i)->getTitle();\n        }\n\n        return $returnValue;\n    }\n","sourceCodeStart":825,"sourceCodeEnd":861,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Spreadsheet.php#L825-L861","documentation":"Spreadsheet::setActiveSheetIndexByName() resolves a sheet title via getSheetByName() and activates it; when no sheet carries that name it throws 'Workbook does not contain sheet:{name}'. Name matching follows getSheetByName's rules (case-insensitive, otherwise exact).","triggerScenarios":"setActiveSheetIndexByName('Summary') with a trailing space in the real title; activating a template tab ('Tabelle1' vs 'Sheet1') in localized files; activating a name computed from variables ('Report ' . $period) that does not match; running after code that renamed or removed the tab.","commonSituations":"Multi-language templates where the first tab's title differs by locale; names assembled from dates/periods where formatting differs (leading zeros, separators); files edited by users who renamed tabs; whitespace introduced when names come from CSV/config cells.","solutions":["Verify existence first: if ($spreadsheet->sheetNameExists($name)) { ... } before activating.","Inspect the real titles: $spreadsheet->getSheetNames(); and trim/normalize your $name to match.","Fall back to activating by index when the name is missing: setActiveSheetIndex(0).","Centralize tab names as constants shared by producer and consumer code instead of literals scattered around."],"exampleFix":"// before\n$spreadsheet->setActiveSheetIndexByName('summary '); // throws\n\n// after\n$name = trim('summary '); // 'summary' matches case-insensitively\nif ($spreadsheet->sheetNameExists($name)) {\n    $spreadsheet->setActiveSheetIndexByName($name);\n} else {\n    $spreadsheet->setActiveSheetIndex(0);\n}","handlingStrategy":"validation","validationCode":"$name = trim($name);\nif ($spreadsheet->sheetNameExists($name)) {\n    $spreadsheet->setActiveSheetIndexByName($name);\n} else {\n    $spreadsheet->setActiveSheetIndex(0);\n}","typeGuard":"function canActivateSheetByName(\\PhpOffice\\PhpSpreadsheet\\Spreadsheet $s, string $name): bool\n{\n    return $s->sheetNameExists(trim($name));\n}","tryCatchPattern":"try {\n    $spreadsheet->setActiveSheetIndexByName($name);\n} catch (\\PhpOffice\\PhpSpreadsheet\\Exception $e) {\n    $spreadsheet->setActiveSheetIndex(0);\n}","preventionTips":["Store tab names in shared constants, not string literals in each call site.","Normalize (trim, case) names from config or user input.","Validate expected tabs right after loading a template."],"tags":["worksheet","sheet-not-found","active-sheet","phpspreadsheet"],"backgroundTag":"sheet-not-found","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}