{"record":{"id":"4a072baa6d9ecc92","repo":"PHPOffice/PhpSpreadsheet","slug":"invalid-character-found-in-sheet-code-name","errorCode":null,"errorMessage":"Invalid character found in sheet code name","messagePattern":"Invalid character found in sheet code name","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Worksheet/Worksheet.php","lineNumber":448,"sourceCode":"     * Check sheet code name for valid Excel syntax.\n     *\n     * @param string $sheetCodeName The string to check\n     *\n     * @return string The valid string\n     */\n    private static function checkSheetCodeName(string $sheetCodeName): string\n    {\n        $charCount = StringHelper::countCharacters($sheetCodeName);\n        if ($charCount == 0) {\n            throw new Exception('Sheet code name cannot be empty.');\n        }\n        // Some of the printable ASCII characters are invalid:  * : / \\ ? [ ] and  first and last characters cannot be a \"'\"\n        if (\n            (str_replace(self::INVALID_CHARACTERS, '', $sheetCodeName) !== $sheetCodeName)\n            || (StringHelper::substring($sheetCodeName, -1, 1) == '\\'')\n            || (StringHelper::substring($sheetCodeName, 0, 1) == '\\'')\n        ) {\n            throw new Exception('Invalid character found in sheet code name');\n        }\n\n        // Enforce maximum characters allowed for sheet title\n        if ($charCount > self::SHEET_TITLE_MAXIMUM_LENGTH) {\n            throw new Exception('Maximum ' . self::SHEET_TITLE_MAXIMUM_LENGTH . ' characters allowed in sheet code name.');\n        }\n\n        return $sheetCodeName;\n    }\n\n    /**\n     * Check sheet title for valid Excel syntax.\n     *\n     * @param string $sheetTitle The string to check\n     *\n     * @return string The valid string\n     */\n    private static function checkSheetTitle(string $sheetTitle): string","sourceCodeStart":430,"sourceCodeEnd":466,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Worksheet/Worksheet.php#L430-L466","documentation":"Thrown by Worksheet::checkSheetCodeName() when the code name contains any of the characters * : / \\ ? [ ] (self::INVALID_CHARACTERS) or begins/ends with a single quote. Excel forbids these in sheet code names, and unlike spaces (which setCodeName converts to underscores) these characters are not auto-repaired, so the library throws.","triggerScenarios":"$sheet->setCodeName('Sales/2024'); setCodeName(\"'TopSheet'\") with surrounding apostrophes; passing a title containing ? or [ unchanged as the code name.","commonSituations":"Reusing a date-bearing title like 'Report 2024/06' as code name; importing names from external systems (file paths, slashes) into code names; generating code names from free-text fields.","solutions":["Sanitize before setting: str_replace(['*','/',':','\\\\','?','[',']'], '_', $codeName) and trim surrounding quotes","Prefer letting the library derive the code name from the (already validated) title instead of setting it manually","Apply the same character policy you use for titles to code names, plus the no-leading/trailing-quote rule"],"exampleFix":"// before\n$sheet->setCodeName('Sales/Europe 2024'); // '/' is invalid\n\n// after\n$clean = str_replace(['*','/',':','\\\\','?','[',']'], '_', 'Sales/Europe 2024');\n$sheet->setCodeName($clean); // 'Sales_Europe_2024' (space -> '_' automatically)","handlingStrategy":"validation","validationCode":"const INVALID = ['*', '/', ':', '\\\\', '?', '[', ']'];\n\n$codeName = str_replace(INVALID, '_', trim($codeName, \"'\\\"\"));\nif ($codeName !== '') {\n    $sheet->setCodeName($codeName);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Run the same sanitizer for titles and code names: replace * : / \\ ? [ ] and strip edge quotes","Let the library derive the code name from the title when you have no strong requirement","Treat external strings (paths, dates, user text) as untrusted for sheet identifiers"],"tags":["phpspreadsheet","worksheet","code-name","invalid-characters","validation"],"backgroundTag":"invalid-characters-in-name","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}