{"record":{"id":"b14d2c574aa67707","repo":"iOfficeAI/OfficeCLI","slug":"column-index-cidx-out-of-range-1-tcols-count","errorCode":null,"errorMessage":"Column index {cIdx} out of range (1..{tCols?.Count ?? 0})","messagePattern":"Column index (.+?) out of range \\(1\\.\\.(.+?)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Query.cs","lineNumber":828,"sourceCode":"            var tableIdx = int.Parse(tableMatch.Groups[1].Value);\n            return TableToNode(sheetNameFromPath, worksheet, tableIdx, depth);\n        }\n\n        // Table column path: /Sheet1/table[N]/columns[M] or /column[M]\n        var tableColMatch = Regex.Match(cellRef,\n            @\"^table\\[(\\d+)\\]/(?:columns|column)\\[(\\d+)\\]$\", RegexOptions.IgnoreCase);\n        if (tableColMatch.Success)\n        {\n            var tIdx = int.Parse(tableColMatch.Groups[1].Value);\n            var cIdx = int.Parse(tableColMatch.Groups[2].Value);\n            var tParts = worksheet.TableDefinitionParts.ToList();\n            if (tIdx < 1 || tIdx > tParts.Count)\n                throw new ArgumentException($\"Table index {tIdx} out of range (1..{tParts.Count})\");\n            var tbl = tParts[tIdx - 1].Table\n                ?? throw new ArgumentException($\"Table {tIdx} has no definition\");\n            var tCols = tbl.GetFirstChild<TableColumns>()?.Elements<TableColumn>().ToList();\n            if (tCols == null || cIdx < 1 || cIdx > tCols.Count)\n                throw new ArgumentException($\"Column index {cIdx} out of range (1..{tCols?.Count ?? 0})\");\n            var tCol = tCols[cIdx - 1];\n            var tcNode = new DocumentNode\n            {\n                Path = $\"/{sheetNameFromPath}/table[{tIdx}]/columns[{cIdx}]\",\n                Type = \"tableColumn\",\n                Text = tCol.Name?.Value ?? \"\"\n            };\n            tcNode.Format[\"name\"] = tCol.Name?.Value ?? \"\";\n            if (tCol.Id?.Value != null) tcNode.Format[\"id\"] = tCol.Id.Value;\n            if (tCol.TotalsRowFunction?.HasValue == true)\n                // Open XML SDK v3 EnumValue<T>.ToString() returns\n                // \"TotalsRowFunctionValues { }\" — use InnerText for the\n                // OOXML-canonical lowercase token. CONSISTENCY(enum-innertext).\n                tcNode.Format[\"totalFunction\"] = tCol.TotalsRowFunction.InnerText;\n            if (tCol.TotalsRowLabel?.Value != null)\n                tcNode.Format[\"totalLabel\"] = tCol.TotalsRowLabel.Value;\n            var ccf = tCol.CalculatedColumnFormula?.Text;\n            if (!string.IsNullOrEmpty(ccf)) tcNode.Format[\"formula\"] = ccf;","sourceCodeStart":810,"sourceCodeEnd":846,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Query.cs#L810-L846","documentation":"Thrown for /Sheet/table[N]/columns[M] when M exceeds the table's TableColumn count (1-based), or when the table has no <tableColumns> element at all. The message reports the valid column range (1..count, or 1..0 if none).","triggerScenarios":"handler.Get(\"/Sheet1/table[1]/columns[5]\") on a table with fewer than 5 columns. columns[0]. A table whose <tableColumns> was stripped.","commonSituations":"Hard-coded column index after columns were added or removed. Zero-based indexing. Mismatch between the table's actual columns and a cached schema.","solutions":["Use a 1-based index in [1, columnCount].","Read the table's column list first to learn the count.","try/catch(ArgumentException) and read the (1..N) range from the message."],"exampleFix":"// before\nvar col = handler.Get(\"/Sheet1/table[1]/columns[5]\"); // throws if <5 columns\n\n// after\ntry { var col = handler.Get(\"/Sheet1/table[1]/columns[5]\"); }\ncatch (ArgumentException) { /* column index invalid */ }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"static int? ElementIndex(string cellRef, string element)\n{\n    var m = Regex.Match(cellRef, $@\"^{Regex.Escape(element)}\\[(\\d+)$\", RegexOptions.IgnoreCase);\n    return m.Success && int.TryParse(m.Groups[1].Value, out var i) ? i : null;\n}","tryCatchPattern":"try { return handler.Get(\"/Sheet1/table[1]/columns[5]\"); }\ncatch (ArgumentException ex) { /* ex.Message carries the valid (1..N) column range */ return null; }","preventionTips":["Column indices are 1-based — there is no index 0.","Read the table's column list before indexing.","Parse the (1..N) range from the exception message."],"tags":["excel","tables","tablecolumn","path-index","argument-exception"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}