{"record":{"id":"0c74c12a8d57d0f3","repo":"iOfficeAI/OfficeCLI","slug":"autofilter-range-afrangeupper-overlaps-existin","errorCode":null,"errorMessage":"AutoFilter range '{afRangeUpper}' overlaps existing table '{existingTable.Name?.Value ?? existingTable.DisplayName?.Value}' ({existingTableRef}); tables already include their own autoFilter.","messagePattern":"AutoFilter range '(.+?)' overlaps existing table '(.+?)' \\((.+?)\\); tables already include their own autoFilter\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.Tables.cs","lineNumber":763,"sourceCode":"                @\"^\\$?[A-Z]+\\$?\\d+(?::\\$?[A-Z]+\\$?\\d+)?$\",\n                RegexOptions.IgnoreCase))\n            throw new ArgumentException(\n                $\"Invalid 'range' value: '{afRange}'. Expected a cell range like 'A1:F100' or 'A1'.\");\n        // Canonicalize inverted input (D5:A1) like the rest of the range family.\n        afRange = NormalizeA1Range(afRange);\n\n        // CONSISTENCY(autofilter-table-dup): a Table already owns its own\n        // <autoFilter> internally; layering a sheet-level <autoFilter> over\n        // the same range produces the duplicate that Excel rejects with a\n        // \"found a problem\" repair dialog. Mirror the T4 overlap check\n        // used by AddTable.\n        var afRangeUpper = afRange.ToUpperInvariant();\n        foreach (var existingTdp in afWorksheet.TableDefinitionParts)\n        {\n            var existingTable = existingTdp.Table;\n            if (existingTable?.Reference?.Value is string existingTableRef\n                && RangesOverlap(afRangeUpper, existingTableRef.ToUpperInvariant()))\n                throw new ArgumentException(\n                    $\"AutoFilter range '{afRangeUpper}' overlaps existing table \" +\n                    $\"'{existingTable.Name?.Value ?? existingTable.DisplayName?.Value}' \" +\n                    $\"({existingTableRef}); tables already include their own autoFilter.\");\n        }\n\n        var wsElement = GetSheet(afWorksheet);\n        var autoFilter = wsElement.GetFirstChild<AutoFilter>();\n        if (autoFilter == null)\n        {\n            autoFilter = new AutoFilter();\n            // AutoFilter goes after SheetData (after MergeCells if present)\n            var mergeCellsEl = wsElement.GetFirstChild<MergeCells>();\n            var sheetDataEl = wsElement.GetFirstChild<SheetData>();\n            if (mergeCellsEl != null)\n                mergeCellsEl.InsertAfterSelf(autoFilter);\n            else if (sheetDataEl != null)\n                sheetDataEl.InsertAfterSelf(autoFilter);\n            else","sourceCodeStart":745,"sourceCodeEnd":781,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.Tables.cs#L745-L781","documentation":"Thrown by AddAutoFilter after the range is canonicalized. It walks afWorksheet.TableDefinitionParts and, for each table with a Reference, calls RangesOverlap on the upper-cased AutoFilter range and the table's reference. A Table already carries its own <autoFilter>; layering a sheet-level <autoFilter> over the same cells duplicates it and Excel shows a 'found a problem' repair dialog, so the add is rejected up front.","triggerScenarios":"Call Add type \"autofilter\" whose range geometrically overlaps a Table's reference on the same sheet.","commonSituations":"Adding a sheet-level AutoFilter that covers a region already occupied by a ListObject table; defaulting the filter to the whole used range that includes a table.","solutions":["Use a non-overlapping range for the sheet-level AutoFilter.","Rely on the table's built-in AutoFilter instead of adding a sheet-level one over it.","Move or shrink the table so the AutoFilter range is disjoint."],"exampleFix":"// before (table occupies A1:F100, AutoFilter covers the same)\nhandler.Add(\"/Sheet1\", \"autofilter\", null, new() { [\"range\"] = \"A1:F100\" });\n// after (filter only the non-table area)\nhandler.Add(\"/Sheet1\", \"autofilter\", null, new() { [\"range\"] = \"H1:K50\" });","handlingStrategy":"try-catch","validationCode":"// Pre-check: list tables on the sheet and reject range overlap before Add.\nvar tableRefs = handler.Query($\"/{sheet}/table\")\n    .Select(n => n.Properties.GetValueOrDefault(\"ref\", \"\"));\nforeach (var tr in tableRefs)\n    if (RangesOverlapLocal(afRange.ToUpperInvariant(), tr.ToUpperInvariant()))\n        throw new InvalidOperationException($\"AutoFilter range overlaps table ref {tr}\");","typeGuard":null,"tryCatchPattern":"try { handler.Add(\"/Sheet1\", \"autofilter\", null, props); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"overlaps existing table\"))\n{\n    // use a disjoint range, or rely on the table's own built-in AutoFilter\n}","preventionTips":["Do not layer a sheet-level AutoFilter over a Table region; tables bring their own filter.","Scope AutoFilter ranges to the non-table area of the sheet.","Dump table refs first and compute a disjoint filter range."],"tags":["excel","autofilter","table","overlap","collision"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}