{"record":{"id":"23f9b2aaa57ed838","repo":"iOfficeAI/OfficeCLI","slug":"table-index-tableindex-out-of-range-1-tablepa","errorCode":null,"errorMessage":"Table index {tableIndex} out of range (1..{tableParts.Count})","messagePattern":"Table index (.+?) out of range \\(1\\.\\.(.+?)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Helpers.Node.cs","lineNumber":1037,"sourceCode":"            else\n                rPr.AppendChild(new Color { Indexed = 81 });\n\n            var fontStr = o[\"font\"]?.GetValue<string>();\n            rPr.AppendChild(new RunFont { Val = string.IsNullOrWhiteSpace(fontStr) ? \"Tahoma\" : fontStr });\n\n            ct.AppendChild(new Run(rPr,\n                new Text(text.Replace(\"\\r\\n\", \"\\n\")) { Space = SpaceProcessingModeValues.Preserve }));\n        }\n        return ct;\n    }\n\n    // ==================== Data Validation Helpers ====================\n\n    private DocumentNode TableToNode(string sheetName, WorksheetPart worksheetPart, int tableIndex, int depth)\n    {\n        var tableParts = worksheetPart.TableDefinitionParts.ToList();\n        if (tableIndex < 1 || tableIndex > tableParts.Count)\n            throw new ArgumentException($\"Table index {tableIndex} out of range (1..{tableParts.Count})\");\n\n        var tbl = tableParts[tableIndex - 1].Table\n            ?? throw new ArgumentException($\"Table {tableIndex} has no definition\");\n\n        var node = new DocumentNode\n        {\n            Path = $\"/{sheetName}/table[{tableIndex}]\",\n            Type = \"table\",\n            Text = tbl.DisplayName?.Value ?? tbl.Name?.Value ?? $\"Table{tableIndex}\",\n            Preview = $\"{tbl.Name?.Value} ({tbl.Reference?.Value})\"\n        };\n\n        node.Format[\"name\"] = tbl.Name?.Value ?? \"\";\n        node.Format[\"displayName\"] = tbl.DisplayName?.Value ?? \"\";\n        node.Format[\"ref\"] = tbl.Reference?.Value ?? \"\";\n\n        var styleInfo = tbl.GetFirstChild<TableStyleInfo>();\n        if (styleInfo?.Name?.Value != null)","sourceCodeStart":1019,"sourceCodeEnd":1055,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Helpers.Node.cs#L1019-L1055","documentation":"Thrown by TableToNode when the requested table index (1-based) is less than 1 or greater than the number of TableDefinitionParts on the worksheet. Used when navigating a worksheet's table children by positional index.","triggerScenarios":"Calling table enumeration/inspection with tableIndex=0, tableIndex=N+1 (one past last), or tableIndex negative against a worksheet that has N TableDefinitionParts; querying a worksheet that has zero tables with any index >= 1.","commonSituations":"Off-by-one from 0-based indexing in the caller; assuming every sheet has a table when it has none; stale index cached before tables were added/removed.","solutions":["Enumerate tables first and bound the index to 1..count before calling.","Use 1-based indexing for table positions (matches the error's range text).","If the sheet has zero tables, do not call TableToNode with any index — short-circuit upstream."],"exampleFix":"// before\nint idx = 0; // wrong — API is 1-based\nTableToNode(sheet, wsPart, idx, 0);\n// after\nvar parts = wsPart.TableDefinitionParts.ToList();\nif (parts.Count == 0) return; // no tables\nint idx = Math.Clamp(idx, 1, parts.Count);\nTableToNode(sheet, wsPart, idx, 0);","handlingStrategy":"validation","validationCode":"// Check table count before address-by-index\nint count = worksheetPart.TableDefinitionParts.Count();\nbool inRange(int idx) => idx >= 1 && idx <= count;","typeGuard":"null","tryCatchPattern":"try { TableToNode(sheet, wsPart, idx, 0); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Table index\") && ex.Message.Contains(\"out of range\"))\n{ /* re-enumerate tables, clamp or report the valid range */ }","preventionTips":["Table indices in this API are 1-based.","Never assume a worksheet has a table — count first.","Refresh cached indices after add/remove table operations."],"tags":["tables","excel","indexing","input-validation"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}