iOfficeAI/OfficeCLI · error · ArgumentException
Table index {tableIndex} out of range (1..{tableParts.Count}
Error message
Table index {tableIndex} out of range (1..{tableParts.Count}) What it means
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.
Source
Thrown at src/officecli/Handlers/Excel/ExcelHandler.Helpers.Node.cs:1037
else
rPr.AppendChild(new Color { Indexed = 81 });
var fontStr = o["font"]?.GetValue<string>();
rPr.AppendChild(new RunFont { Val = string.IsNullOrWhiteSpace(fontStr) ? "Tahoma" : fontStr });
ct.AppendChild(new Run(rPr,
new Text(text.Replace("\r\n", "\n")) { Space = SpaceProcessingModeValues.Preserve }));
}
return ct;
}
// ==================== Data Validation Helpers ====================
private DocumentNode TableToNode(string sheetName, WorksheetPart worksheetPart, int tableIndex, int depth)
{
var tableParts = worksheetPart.TableDefinitionParts.ToList();
if (tableIndex < 1 || tableIndex > tableParts.Count)
throw new ArgumentException($"Table index {tableIndex} out of range (1..{tableParts.Count})");
var tbl = tableParts[tableIndex - 1].Table
?? throw new ArgumentException($"Table {tableIndex} has no definition");
var node = new DocumentNode
{
Path = $"/{sheetName}/table[{tableIndex}]",
Type = "table",
Text = tbl.DisplayName?.Value ?? tbl.Name?.Value ?? $"Table{tableIndex}",
Preview = $"{tbl.Name?.Value} ({tbl.Reference?.Value})"
};
node.Format["name"] = tbl.Name?.Value ?? "";
node.Format["displayName"] = tbl.DisplayName?.Value ?? "";
node.Format["ref"] = tbl.Reference?.Value ?? "";
var styleInfo = tbl.GetFirstChild<TableStyleInfo>();
if (styleInfo?.Name?.Value != null)View on GitHub (pinned to 1ced45e900)
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.
Example fix
// before int idx = 0; // wrong — API is 1-based TableToNode(sheet, wsPart, idx, 0); // after var parts = wsPart.TableDefinitionParts.ToList(); if (parts.Count == 0) return; // no tables int idx = Math.Clamp(idx, 1, parts.Count); TableToNode(sheet, wsPart, idx, 0);
Defensive patterns
Strategy: validation
Validate before calling
// Check table count before address-by-index int count = worksheetPart.TableDefinitionParts.Count(); bool inRange(int idx) => idx >= 1 && idx <= count;
Type guard
null
Try / catch
try { TableToNode(sheet, wsPart, idx, 0); }
catch (ArgumentException ex) when (ex.Message.Contains("Table index") && ex.Message.Contains("out of range"))
{ /* re-enumerate tables, clamp or report the valid range */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Table name '{name}' is not a valid Excel name: use letters,
- Unknown table style: '{styleName}'. Use a built-in name like
- Invalid '{propertyName}' value '{value}'. Expected a non-neg
- Invalid color value: '{value}'. Expected 6-digit hex RGB (e.
- Invalid source range: {sourceRef}
AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13).
Data as JSON: /api/errors/23f9b2aaa57ed838.
Report an issue: GitHub.