{"record":{"id":"179f6039430264f7","repo":"iOfficeAI/OfficeCLI","slug":"invalid-sheet-name-name-cannot-start-or-end-w","errorCode":null,"errorMessage":"Invalid sheet name '{name}': cannot start or end with an apostrophe (').","messagePattern":"Invalid sheet name '(.+?)': cannot start or end with an apostrophe \\('\\)\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Helpers.Validation.cs","lineNumber":332,"sourceCode":"                    \"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\n    /// \"=[1]Sheet1!A1\" need an externalLinks part to resolve. Without one,\n    /// Excel opens the file but the formula shows #REF!. Reject up-front\n    /// rather than silently persist a broken formula.\n    /// CONSISTENCY(cross-workbook-ref): mirrors the namedrange refersTo\n    /// guard in ExcelHandler.Add.Tables.cs (R27-1).\n    /// </summary>\n    internal static void RejectCrossWorkbookFormula(string formula)\n    {\n        if (string.IsNullOrEmpty(formula)) return;\n        var trimmed = formula.TrimStart('=', ' ', '\\t');","sourceCodeStart":314,"sourceCodeEnd":350,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Helpers.Validation.cs#L314-L350","documentation":"Excel rejects sheet names that start or end with an apostrophe because the single quote is the escape character used to quote sheet names containing spaces or symbols in formulas (e.g. 'My Data'!A1). A leading/trailing quote would make such references ambiguous.","triggerScenarios":"A sheet name whose first or last character is the apostrophe ('). The StartsWith('') / EndsWith('') check trips after the forbidden-char check.","commonSituations":"Copy-paste artifacts that drag in a trailing quote; typographic apostrophes converted to straight quotes; data labels that begin or end with a possessive.","solutions":["Trim leading and trailing apostrophes from the name.","Validate at the input boundary and strip stray quotes.","If the quote is meaningful, embed it inside the name rather than at an edge."],"exampleFix":"// before\nwb.AddSheet(\"'Q1 Data'\");\n\n// after\nvar name = userSheetName.Trim(' \\'');\nwb.AddSheet(name);","handlingStrategy":"validation","validationCode":"static string TrimApostropheEdges(string n) =>\n    (n.StartsWith('\\'') || n.EndsWith('\\'')) ? n.Trim('\\'') : n;","typeGuard":null,"tryCatchPattern":"try { wb.AddSheet(name); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"apostrophe\")) {\n    wb.AddSheet(name.Trim('\\''));\n}","preventionTips":["Strip stray quotes at the input boundary along with whitespace.","Watch for typographic apostrophes converted to ASCII quotes by copy-paste."],"tags":["excel","sheet-name","validation","apostrophe"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}