{"record":{"id":"bc21d4e8acde5fac","repo":"PHPOffice/PhpSpreadsheet","slug":"sheet-code-name-cannot-be-empty","errorCode":null,"errorMessage":"Sheet code name cannot be empty.","messagePattern":"Sheet code name cannot be empty\\.","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Worksheet/Worksheet.php","lineNumber":440,"sourceCode":"     * @return string[]\n     */\n    public static function getInvalidCharacters(): array\n    {\n        return self::INVALID_CHARACTERS;\n    }\n\n    /**\n     * 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","sourceCodeStart":422,"sourceCodeEnd":458,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Worksheet/Worksheet.php#L422-L458","documentation":"Thrown by Worksheet::checkSheetCodeName() (via setCodeName(), when validation is enabled) when the code name string has zero characters. A worksheet's code name is written into the xlsx XML as sheet codeName, and Excel rejects empty values, so the library fails fast. Note setCodeName() converts spaces to underscores but cannot rescue an empty string.","triggerScenarios":"$sheet->setCodeName(''); passing a trimmed user input that is '' ; calling setCodeName($var) where $var was initialized but never assigned. (Readers pass validate=false, so this comes from userland calls.)","commonSituations":"Deriving code names from optional user/profile fields that can be empty; refactorings that moved the setCodeName call before the variable is populated.","solutions":["Provide a non-empty code name, e.g. $sheet->setCodeName('Sheet1_Code')","Or simply do not call setCodeName — the writer generates one from the title when none is set","Default the value: $sheet->setCodeName($codeName !== '' ? $codeName : $sheet->getTitle())"],"exampleFix":"// before\n$sheet->setCodeName(trim($request->get('codeName'))); // '' when field empty\n\n// after\n$codeName = trim($request->get('codeName'));\n$sheet->setCodeName($codeName !== '' ? $codeName : 'Sheet_' . $sheetIndex);","handlingStrategy":"validation","validationCode":"if ($codeName === '') {\n    $codeName = 'Sheet_' . $sheetIndex;\n}\n$sheet->setCodeName($codeName);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Don't call setCodeName unless you actually need a specific code name","Default empty inputs to a generated slug before calling setCodeName","Keep title and code-name sanitation in one shared helper"],"tags":["phpspreadsheet","worksheet","code-name","empty-string","validation"],"backgroundTag":"empty-required-field","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}