{"record":{"id":"b53b1a4ccc501d41","repo":"iOfficeAI/OfficeCLI","slug":"invalid-sheet-name-name-cannot-be-empty-or-whites","errorCode":null,"errorMessage":"Invalid sheet name: name cannot be empty or whitespace.","messagePattern":"Invalid sheet name: name cannot be empty or whitespace\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Helpers.Validation.cs","lineNumber":322,"sourceCode":"            var col = m.Groups[1].Value.ToUpperInvariant();\n            if (!long.TryParse(m.Groups[2].Value, out var row)) continue;\n            // Column index check: ColumnNameToIndex would throw on overflow,\n            // but we want a clean validation message. Compute manually.\n            int colIdx = 0;\n            foreach (var ch in col) colIdx = colIdx * 26 + (ch - 'A' + 1);\n            if (colIdx < 1 || colIdx > 16384 || row < 1 || row > 1048576)\n            {\n                throw new ArgumentException(\n                    $\"Formula contains out-of-range cell reference '{m.Value}'. \" +\n                    \"Excel limits: rows 1-1048576, columns A-XFD.\");\n            }\n        }\n    }\n\n    internal static void ValidateSheetName(string name)\n    {\n        if (string.IsNullOrWhiteSpace(name))\n            throw new ArgumentException(\"Invalid sheet name: name cannot be empty or whitespace.\");\n        if (name.Length > 31)\n            throw new ArgumentException(\n                $\"Invalid sheet name '{name}': length {name.Length} exceeds Excel's 31-char limit.\");\n        var forbidden = new[] { '\\\\', '/', '?', '*', ':', '[', ']' };\n        var hit = name.IndexOfAny(forbidden);\n        if (hit >= 0)\n            throw new ArgumentException(\n                $\"Invalid sheet name '{name}': contains forbidden character '{name[hit]}'. Excel rejects any of: \\\\ / ? * : [ ]\");\n        if (name.StartsWith('\\'') || name.EndsWith('\\''))\n            throw new ArgumentException(\n                $\"Invalid sheet name '{name}': cannot start or end with an apostrophe (').\");\n        if (name.Equals(\"History\", StringComparison.OrdinalIgnoreCase))\n            throw new ArgumentException(\n                \"Invalid sheet name 'History': reserved by Excel for the change-history sheet.\");\n    }\n\n    /// <summary>\n    /// R35-3: cross-workbook cell formulas like \"=[Other.xlsx]Sheet1!A1\" or","sourceCodeStart":304,"sourceCodeEnd":340,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Helpers.Validation.cs#L304-L340","documentation":"ValidateSheetName was called with null, empty, or whitespace-only input. Excel requires every worksheet to carry a non-empty name, so the library rejects the value before it can reach the OOXML writer. This is the first guard in the sheet-name validation chain.","triggerScenarios":"Any API that creates or renames a sheet (AddSheet, rename, copy/move target) receiving string.Empty, \"   \", or null. The string.IsNullOrWhiteSpace check trips before length/character checks run.","commonSituations":"User input field left blank; a variable never assigned; a .Trim() that reduced the value to empty; reading a name from config that was omitted.","solutions":["Pass a non-empty, trimmed name.","Default to a generated unique name (e.g. 'Sheet1', 'Sheet2') when the source value is blank.","Guard at the input boundary so blank never reaches the sheet API."],"exampleFix":"// before\nwb.AddSheet(userTitle.Trim());   // userTitle was all spaces -> \"\"\n\n// after\nvar name = string.IsNullOrWhiteSpace(userTitle) ? $\"Sheet{wb.SheetCount+1}\" : userTitle.Trim();\nwb.AddSheet(name);","handlingStrategy":"validation","validationCode":"static string EnsureSheetName(string? raw, int fallbackIndex) {\n    var n = (raw ?? string.Empty).Trim();\n    return string.IsNullOrWhiteSpace(n) ? $\"Sheet{fallbackIndex}\" : n;\n}","typeGuard":null,"tryCatchPattern":"try { wb.AddSheet(name); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"cannot be empty\")) {\n    name = $\"Sheet{wb.SheetCount + 1}\";\n    wb.AddSheet(name);\n}","preventionTips":["Never pass user input straight to AddSheet; route it through a normalizer.","Treat blank as 'generate a default', not as 'throw'."],"tags":["excel","sheet-name","validation"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}