{"record":{"id":"dd9021bbbd57d819","repo":"iOfficeAI/OfficeCLI","slug":"merge-range-refupper-overlaps-listobject-table","errorCode":null,"errorMessage":"Merge range '{refUpper}' overlaps ListObject table '{tblName}' (ref '{tblRef}'). Excel does not allow merging cells inside a table range. Convert the table to a normal range first.","messagePattern":"Merge range '(.+?)' overlaps ListObject table '(.+?)' \\(ref '(.+?)'\\)\\. Excel does not allow merging cells inside a table range\\. Convert the table to a normal range first\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Helpers.Cell.cs","lineNumber":305,"sourceCode":"                    $\"Merge range '{refUpper}' overlaps existing merged range '{er}'. \" +\n                    $\"Excel rejects overlapping mergeCell entries.\");\n        }\n        // BUG-R2-table-merge BUG-5: Excel forbids mergeCell entries that\n        // intersect a ListObject table range — files saved with such a\n        // merge open with a \"found a problem\" repair dialog. Reject up\n        // front so callers see a clear error instead of file corruption.\n        if (worksheetPart != null)\n        {\n            foreach (var tdp in worksheetPart.TableDefinitionParts)\n            {\n                var tblRef = tdp.Table?.Reference?.Value;\n                if (string.IsNullOrEmpty(tblRef)) continue;\n                if (RangesOverlap(refUpper, tblRef.ToUpperInvariant()))\n                {\n                    var tblName = tdp.Table?.Name?.Value\n                        ?? tdp.Table?.DisplayName?.Value\n                        ?? \"(unnamed)\";\n                    throw new ArgumentException(\n                        $\"Merge range '{refUpper}' overlaps ListObject table '{tblName}' (ref '{tblRef}'). \" +\n                        \"Excel does not allow merging cells inside a table range. Convert the table to a normal range first.\");\n                }\n            }\n        }\n        // Advisory only — a multi-million-cell merge is legal OOXML and\n        // validates green, but real Excel stalls for minutes opening it\n        // (empirically: 16384x1 renders in seconds; 1024x100000 hangs past\n        // a 120s render timeout). Warn so the caller knows the file may be\n        // unusable in practice; do not reject (lenient-input convention).\n        if (TryParseRangeDims(refUpper, out var mergeRows, out var mergeCols)\n            && (long)mergeRows * mergeCols > 10_000_000)\n        {\n            Console.Error.WriteLine(\n                $\"Warning: merge range '{refUpper}' spans {(long)mergeRows * mergeCols:N0} cells; \" +\n                \"real Excel can hang for minutes opening merges this large.\");\n        }\n        mergeCells.AppendChild(new MergeCell { Reference = refUpper });","sourceCodeStart":287,"sourceCodeEnd":323,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Helpers.Cell.cs#L287-L323","documentation":"InsertMergeCellCheckedCore rejects a merge range that intersects a ListObject (table) range: Excel opens such files with a 'found a problem' repair dialog, so this prevents silent corruption. It iterates worksheetPart.TableDefinitionParts and tests each Table.Reference for overlap with the merge. Only fires when worksheetPart is supplied (the Set merge path passes it). The error names the offending table and its reference.","triggerScenarios":"Table defined over A1:C5, then merge B2:B3 (inside the table); merge any cell range that intersects a table's Reference rectangle.","commonSituations":"Treating a table region like a normal range and merging header cells inside it; forgetting to convert an imported table to a normal range before merging.","solutions":["Convert the table to a normal range first (Excel: Table Design > Convert to Range), then merge.","Merge cells outside the table's Reference rectangle.","Remove the table if merging within its area is essential.","Check table ranges (worksheetPart.TableDefinitionParts) before issuing the merge."],"exampleFix":"// before -- B2:B3 lies inside table 'SalesData' (A1:E10)\nset sheet!B2:B3 merge=true  // throws: overlaps ListObject\n\n// after -- merge outside the table, or remove the table first\nset sheet!G2:G3 merge=true","handlingStrategy":"validation","validationCode":"// Before merging, confirm the range does not intersect any table range.\nforeach (var tbl in handler.GetTablesOnSheet(sheet)) // your helper returning refs\n    if (RangesOverlap(newRange, tbl.Reference))\n        throw new InvalidOperationException($\"Merge intersects table {tbl.Name} ({tbl.Reference}); convert to range first.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never merge inside a ListObject table range; convert the table to a range first.","Check TableDefinitionParts references before issuing a merge.","Keep merge regions and table regions disjoint by construction."],"tags":["excel","ooxml","merge","table","validation"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}