{"record":{"id":"cd879ba897f14d95","repo":"iOfficeAI/OfficeCLI","slug":"defined-name-nrname-collides-with-the-table-na","errorCode":null,"errorMessage":"Defined name '{nrName}' collides with the table name '{existingTable.Name?.Value ?? existingTable.DisplayName?.Value}'. Excel requires table and defined names to be unique in one namespace; pick a different name.","messagePattern":"Defined name '(.+?)' collides with the table name '(.+?)'\\. Excel requires table and defined names to be unique in one namespace; pick a different name\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.Tables.cs","lineNumber":207,"sourceCode":"            if (!string.Equals(existingName, nrName, StringComparison.OrdinalIgnoreCase)) continue;\n            if (existingDn.LocalSheetId?.Value == dnLocalId)\n                throw new ArgumentException(\n                    $\"Defined name '{nrName}' already exists\" +\n                    (dnLocalId.HasValue ? $\" in sheet scope (localSheetId={dnLocalId})\" : \" in workbook scope\") +\n                    \"; remove it before adding a new one or pick a different name.\");\n        }\n\n        // Mirror of the table-side check: Excel's name namespace spans\n        // defined names AND ListObject table names; a collision passes\n        // schema validation but real Excel refuses the file (0x800A03EC).\n        foreach (var existingTable in _doc.WorkbookPart!.WorksheetParts\n            .SelectMany(wp => wp.TableDefinitionParts)\n            .Select(tdp => tdp.Table)\n            .Where(t => t != null)!)\n        {\n            if (string.Equals(existingTable!.Name?.Value, nrName, StringComparison.OrdinalIgnoreCase)\n                || string.Equals(existingTable.DisplayName?.Value, nrName, StringComparison.OrdinalIgnoreCase))\n                throw new ArgumentException(\n                    $\"Defined name '{nrName}' collides with the table name '{existingTable.Name?.Value ?? existingTable.DisplayName?.Value}'. Excel requires table and defined names to be unique in one namespace; pick a different name.\");\n        }\n\n        definedNames.AppendChild(dn);\n\n        // R7-3: if the defined-name body is a formula (not just a pure\n        // range reference), set fullCalcOnLoad so Excel recomputes on\n        // first open — otherwise the name evaluates to 0 until the\n        // user triggers a recalc.\n        if (LooksLikeFormulaBody(refVal))\n        {\n            var calcPr = workbook.GetFirstChild<CalculationProperties>();\n            if (calcPr == null)\n            {\n                calcPr = new CalculationProperties();\n                var insertBefore = (DocumentFormat.OpenXml.OpenXmlElement?)workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.OleSize>()\n                    ?? (DocumentFormat.OpenXml.OpenXmlElement?)workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.CustomWorkbookViews>()\n                    ?? (DocumentFormat.OpenXml.OpenXmlElement?)workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.PivotCaches>();","sourceCodeStart":189,"sourceCodeEnd":225,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.Tables.cs#L189-L225","documentation":"Thrown by AddNamedRange after the new defined name is built but before it is appended. It scans every TableDefinitionPart across all worksheet parts and compares the candidate name against each table's Name and DisplayName (OrdinalIgnoreCase). In OOXML, defined names and ListObject table names share one namespace; a clash passes schema validation but real Excel refuses the file with 0x800A03EC, so the handler rejects it up front instead of writing a corrupt workbook.","triggerScenarios":"Call handler.Add(parentPath, type:\"namedrange\" (or \"definedname\"/\"name\"), ..., properties with name=X) when any table whose Name or DisplayName equals X already exists anywhere in the workbook.","commonSituations":"Auto-generated range names that reuse a table name; a pipeline that adds a table and then a same-named defined range in the same run; importing a dataset whose header string becomes both a table name and a named range.","solutions":["Pick a distinct name= value for the named range (e.g. prefix it with rng_).","Rename or remove the colliding table first via Set on its displayName or Remove of the table path.","Keep a single source of truth for names so tables and defined ranges never share an identifier."],"exampleFix":"// before\nhandler.Add(\"/namedrange\", \"namedrange\", null,\n    new() { [\"name\"] = \"Sales\", [\"ref\"] = \"Sheet1!$A$1:$D$10\" });\n// table 'Sales' already exists -> collision\n// after\nhandler.Add(\"/namedrange\", \"namedrange\", null,\n    new() { [\"name\"] = \"rng_Sales\", [\"ref\"] = \"Sheet1!$A$1:$D$10\" });","handlingStrategy":"try-catch","validationCode":"string nrName = properties.GetValueOrDefault(\"name\", \"\");\nvar taken = _doc.WorkbookPart!.WorksheetParts\n    .SelectMany(wp => wp.TableDefinitionParts)\n    .Select(tdp => tdp.Table)\n    .Where(t => t != null)\n    .SelectMany(t => new[] { t!.Name?.Value, t.DisplayName?.Value });\nif (taken.Any(n => n != null && n.Equals(nrName, StringComparison.OrdinalIgnoreCase)))\n    throw new InvalidOperationException($\"name '{nrName}' is already a table name\";","typeGuard":null,"tryCatchPattern":"try { handler.Add(\"/namedrange\", \"namedrange\", null, props); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"collides with the table name\"))\n{\n    // prompt for a new name or rename/remove the table, then retry once\n}","preventionTips":["Maintain one name registry covering both tables and defined ranges.","Prefix generated defined-range names (e.g. rng_) to keep namespaces disjoint.","Before bulk adds, list existing table names and diff against your candidate names."],"tags":["excel","ooxml","named-range","table","naming-collision","validation"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}