{"record":{"id":"35291828845b9a54","repo":"iOfficeAI/OfficeCLI","slug":"table-name-tablename-already-exists-in-workboo","errorCode":null,"errorMessage":"Table name '{tableName}' already exists in workbook; choose a different name.","messagePattern":"Table name '(.+?)' already exists in workbook; choose a different name\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.Tables.cs","lineNumber":1108,"sourceCode":"        // displayName defaults to the (already-sanitized) tableName; if\n        // name was user-provided it flows through verbatim so Excel\n        // shows the same identifier the user asked for.\n        var userProvidedDisplay = properties.ContainsKey(\"displayName\");\n        var displayName = SanitizeTableIdentifier(\n            properties.GetValueOrDefault(\"displayName\", tableName),\n            userProvided: userProvidedDisplay || userProvidedName);\n\n        // CONSISTENCY(table-name-unique): Excel requires both name and\n        // displayName to be unique workbook-wide. A duplicate across\n        // sheets surfaces a \"found a problem\" repair dialog. Walk every\n        // WorksheetPart's tables, comparing case-insensitively.\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, tableName, StringComparison.OrdinalIgnoreCase))\n                throw new ArgumentException(\n                    $\"Table name '{tableName}' already exists in workbook; choose a different name.\");\n            if (string.Equals(existingTable.DisplayName?.Value, displayName, StringComparison.OrdinalIgnoreCase))\n                throw new ArgumentException(\n                    $\"Table displayName '{displayName}' already exists in workbook; choose a different displayName.\");\n        }\n        // Excel's name uniqueness spans ListObjects AND workbook defined\n        // names in one namespace — a collision passes schema validation but\n        // real Excel refuses the file (0x800A03EC).\n        var definedNames = _doc.WorkbookPart.Workbook?.DefinedNames;\n        if (definedNames != null)\n        {\n            foreach (var dn in definedNames.Elements<DefinedName>())\n            {\n                var dnName = dn.Name?.Value;\n                if (dnName != null\n                    && (string.Equals(dnName, tableName, StringComparison.OrdinalIgnoreCase)\n                        || string.Equals(dnName, displayName, StringComparison.OrdinalIgnoreCase)))\n                    throw new ArgumentException(","sourceCodeStart":1090,"sourceCodeEnd":1126,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.Tables.cs#L1090-L1126","documentation":"Thrown by AddTable's workbook-wide uniqueness check when a table with the same Name (case-insensitive) already exists on any sheet. Excel requires both Name and DisplayName to be unique across the whole workbook; a duplicate surfaces a 'found a problem' repair dialog. The handler walks every WorksheetPart's TableDefinitionParts to enforce this before writing.","triggerScenarios":"Adding a table whose 'name' property matches an existing table's Name on any sheet, e.g. name=Sales when Sales already exists, including case-only differences (SALES vs sales).","commonSituations":"Re-running a script without unique names, copying a sheet that already contains the table, or assuming name uniqueness is per-sheet.","solutions":["Choose a different, workbook-unique name.","Remove the existing table first if the name should be reused.","Omit 'name' to let the handler auto-generate a unique TableN name."],"exampleFix":"// before\nadd /Sheet2/table --prop name=Sales --prop ref=A1:D10\n// after\nadd /Sheet2/table --prop name=Sales2024 --prop ref=A1:D10","handlingStrategy":"validation","validationCode":"// Reserve a workbook-unique table name before Add.\nstatic string UniqueTableName(ExcelHandler h, string baseName)\n{\n    var taken = new HashSet<string>(h.ListTableNames(), StringComparer.OrdinalIgnoreCase); // pseudo\n    var name = baseName; int i = 2;\n    while (taken.Contains(name)) name = $\"{baseName}{i++}\";\n    return name;\n}","typeGuard":null,"tryCatchPattern":"try { handler.Add(parentPath, \"table\", null, props); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"already exists in workbook\"))\n{ /* generate a unique name and retry */ }","preventionTips":["Maintain a used-names set (tables + defined names) across the workbook when scripting.","Compare case-insensitively; SALES and sales collide.","Omit 'name' to let the handler auto-generate TableN when uniqueness is uncertain."],"tags":["excel","table","validation","naming","uniqueness"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}