{"record":{"id":"26818d18e4491a58","repo":"PHPOffice/PhpSpreadsheet","slug":"maximum-31-characters-allowed-in-sheet-code-name","errorCode":null,"errorMessage":"Maximum 31 characters allowed in sheet code name.","messagePattern":"Maximum 31 characters allowed in sheet code name\\.","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Worksheet/Worksheet.php","lineNumber":453,"sourceCode":"     */\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\n    {\n        // Some of the printable ASCII characters are invalid:  * : / \\ ? [ ]\n        if (str_replace(self::INVALID_CHARACTERS, '', $sheetTitle) !== $sheetTitle) {\n            throw new Exception('Invalid character found in sheet title');\n        }","sourceCodeStart":435,"sourceCodeEnd":471,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Worksheet/Worksheet.php#L435-L471","documentation":"Thrown by Worksheet::checkSheetCodeName() when the code name exceeds self::SHEET_TITLE_MAXIMUM_LENGTH (31) characters. Excel enforces the same 31-character ceiling on code names as on titles; setCodeName() itself only auto-trims when de-duplicating (down to 29/28/27 to append _N), so a too-long first-time name throws.","triggerScenarios":"$sheet->setCodeName(str_repeat('a', 32)); concatenating department + region + year into a code name that crosses 31 chars; passing a full sentence as code name.","commonSituations":"Machine-generated identifiers (prefixed UUIDs, long enum labels) used as code names; localized names whose UTF-8 length is misjudged because strlen was used instead of a character count.","solutions":["Truncate to at most 31 characters (multibyte-safe): $sheet->setCodeName(mb_substr($codeName, 0, 31))","Keep code names short and structural ('Sheet1_Data'), not descriptive sentences","Check StringHelper::countCharacters / mb_strlen($codeName) <= 31 before setting"],"exampleFix":"// before\n$sheet->setCodeName('QuarterlyRegionalSalesReportForAllEuropeanMarkets'); // 54 chars\n\n// after\n$sheet->setCodeName(mb_substr('QuarterlyRegionalSalesReportForAllEuropeanMarkets', 0, 31));","handlingStrategy":"validation","validationCode":"$codeName = mb_substr($codeName, 0, 31);\n$sheet->setCodeName($codeName);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Cap generated code names at 31 characters at the source","Use mb_strlen (character count), not strlen, when checking lengths","Prefer short structural code names ('Data_1') over descriptive sentences"],"tags":["phpspreadsheet","worksheet","code-name","length-limit","validation"],"backgroundTag":"name-length-limit-exceeded","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}