{"record":{"id":"04d0951e1dd3e205","repo":"iOfficeAI/OfficeCLI","slug":"invalid-defined-name-nrname-name-parses-as-a","errorCode":null,"errorMessage":"Invalid defined-name '{nrName}': name parses as a cell reference; choose a different name.","messagePattern":"Invalid defined-name '(.+?)': name parses as a cell reference; choose a different name\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.Tables.cs","lineNumber":61,"sourceCode":"        if (string.IsNullOrEmpty(nrName))\n            throw new ArgumentException(\"'name' property is required for namedrange\");\n        // Per OOXML §18.2.5: defined-name identifiers must start with\n        // letter/underscore/backslash, contain only letter/digit/\n        // underscore/period/backslash, and must not parse as a cell\n        // reference. Otherwise Excel rejects the file with 0x800A03EC.\n        // \"Letter\" is any Unicode letter (\\p{L}) — Excel accepts CJK/\n        // Cyrillic/etc. names; the previous ASCII-only class falsely\n        // rejected them. Emoji/symbols stay rejected (not \\p{L}).\n        if (!System.Text.RegularExpressions.Regex.IsMatch(nrName, @\"^[\\p{L}_\\\\][\\p{L}\\p{N}_\\\\.]*$\"))\n            throw new ArgumentException($\"Invalid defined-name '{nrName}': must start with a letter/underscore and contain only letters, digits, underscores, or periods (no spaces).\");\n        // Excel caps defined-name identifier length at 255 characters; longer\n        // names are silently truncated on open (or the file is rejected with\n        // 0x800A03EC depending on host). Refuse up front instead of letting\n        // a 256+ char name land on disk and round-trip-differ on re-open.\n        if (nrName.Length > 255)\n            throw new ArgumentException($\"Invalid defined-name '{nrName}': length {nrName.Length} exceeds the Excel 255-character limit.\");\n        if (LooksLikeCellReference(nrName))\n            throw new ArgumentException($\"Invalid defined-name '{nrName}': name parses as a cell reference; choose a different name.\");\n        // R39-5: Excel reserves the single letters R and C (case-insensitive)\n        // because they collide with R1C1 reference notation. Excel rejects\n        // the file with 0x800A03EC if either is used as a defined name.\n        if (nrName.Length == 1 && (nrName[0] == 'R' || nrName[0] == 'r' || nrName[0] == 'C' || nrName[0] == 'c'))\n            throw new ArgumentException($\"Invalid defined-name '{nrName}': single letter 'R' / 'C' is reserved by Excel for R1C1 reference notation; choose a different name.\");\n        // `refersTo` is the common Excel-documented alias for `ref`;\n        // silently map it so users don't end up with an empty\n        // <x:definedName/> that corrupts the file.\n        var refVal = properties.GetValueOrDefault(\"ref\",\n            properties.GetValueOrDefault(\"refersTo\",\n                properties.GetValueOrDefault(\"formula\", \"\")));\n        // R15/bt-2: reject up-front when the required ref/refersTo/formula\n        // value is missing so an empty <x:definedName/> never gets written\n        // (the resulting zombie polluted the workbook and broke later Set\n        // calls). Unsupported aliases like `range=` previously silently\n        // landed here as empty and produced the zombie.\n        if (string.IsNullOrEmpty(refVal))\n            throw new ArgumentException(\"'ref' (or 'refersTo' / 'formula') property is required for namedrange\");","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.Tables.cs#L43-L79","documentation":"Thrown when the resolved name parses as a valid A1-style cell reference (LooksLikeCellReference returns true). A defined name that looks like a cell address (e.g. `A1`, `TBL1`, `XFD1048576` within grid bounds) is ambiguous with a cell address, and Excel refuses the file. The check validates the column letters against the real grid (1..16384) and the row against 1..1048576, so `A1` and `IV999` fail but `AAAA1` (column beyond XFD) would not.","triggerScenarios":"Passing `name=A1`, `name=SUM1`, `name=TBL1`, or any 1-3-letter+digits token that falls inside the grid. Common when a desired name happens to spell like a cell.","commonSituations":"Naming a range after its first cell (`A1`); short names like `C5`; names that coincidentally match a cell pattern after uppercase normalization.","solutions":["Choose a name that is not parseable as a cell address, e.g. prefix it: `Range_A1`, `CellA1`, or use a word like `TotalA1`.","If you need a single-letter-ish name, add an underscore or a second word so it no longer matches ^[A-Za-z]{1,3}\\d+$."],"exampleFix":"// before\nadd ./book.xlsx /namedrange --type namedrange --prop name=A1 --prop ref=Sheet1!A1:B1\n// after\nadd ./book.xlsx /namedrange --type namedrange --prop name=RangeA1 --prop ref=Sheet1!A1:B1","handlingStrategy":"validation","validationCode":"if (LooksLikeCellReference(name))\n    throw new InvalidOperationException(\n        $\"Defined-name '{name}' looks like a cell reference; pick another\");","typeGuard":"static bool LooksLikeCellReference(string? s)\n{\n    if (string.IsNullOrEmpty(s)) return false;\n    var m = System.Text.RegularExpressions.Regex.Match(s, @\"^\\$?([A-Za-z]{1,3})\\$?([0-9]+)$\");\n    if (!m.Success) return false;\n    int col = 0; foreach (var ch in m.Groups[1].Value.ToUpperInvariant()) col = col*26 + (ch-'A'+1);\n    return col is >= 1 and <= 16384\n        && long.TryParse(m.Groups[2].Value, out var row) && row is >= 1 and <= 1048576;\n}","tryCatchPattern":"try { handler.AddNamedRange(...); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"parses as a cell reference\"))\n{ /* prefix or rename the identifier */ }","preventionTips":["Avoid 1-3-letter-plus-digits names that fall inside the grid (A1, IV999).","Prefix short names (Range_A1) to disambiguate from cell addresses."],"tags":["excel","named-range","defined-name","cell-reference","input-validation"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}