iOfficeAI/OfficeCLI · error · ArgumentException

Table index {tIdx} out of range (1..{tParts.Count})

Error message

Table index {tIdx} out of range (1..{tParts.Count})

What it means

Thrown for /Sheet/table[N]/columns[M] when N exceeds the sheet's TableDefinitionParts.Count (1-based). Each worksheet owns its own table-definition parts, so the valid range is per-sheet (shown in the message as 1..count). A workbook-global table index won't match.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Query.cs:823

        // Table path: /Sheet1/table[N]
        var tableMatch = Regex.Match(cellRef, @"^table\[(\d+)\]$", RegexOptions.IgnoreCase);
        if (tableMatch.Success)
        {
            var tableIdx = int.Parse(tableMatch.Groups[1].Value);
            return TableToNode(sheetNameFromPath, worksheet, tableIdx, depth);
        }

        // Table column path: /Sheet1/table[N]/columns[M] or /column[M]
        var tableColMatch = Regex.Match(cellRef,
            @"^table\[(\d+)\]/(?:columns|column)\[(\d+)\]$", RegexOptions.IgnoreCase);
        if (tableColMatch.Success)
        {
            var tIdx = int.Parse(tableColMatch.Groups[1].Value);
            var cIdx = int.Parse(tableColMatch.Groups[2].Value);
            var tParts = worksheet.TableDefinitionParts.ToList();
            if (tIdx < 1 || tIdx > tParts.Count)
                throw new ArgumentException($"Table index {tIdx} out of range (1..{tParts.Count})");
            var tbl = tParts[tIdx - 1].Table
                ?? throw new ArgumentException($"Table {tIdx} has no definition");
            var tCols = tbl.GetFirstChild<TableColumns>()?.Elements<TableColumn>().ToList();
            if (tCols == null || cIdx < 1 || cIdx > tCols.Count)
                throw new ArgumentException($"Column index {cIdx} out of range (1..{tCols?.Count ?? 0})");
            var tCol = tCols[cIdx - 1];
            var tcNode = new DocumentNode
            {
                Path = $"/{sheetNameFromPath}/table[{tIdx}]/columns[{cIdx}]",
                Type = "tableColumn",
                Text = tCol.Name?.Value ?? ""
            };
            tcNode.Format["name"] = tCol.Name?.Value ?? "";
            if (tCol.Id?.Value != null) tcNode.Format["id"] = tCol.Id.Value;
            if (tCol.TotalsRowFunction?.HasValue == true)
                // Open XML SDK v3 EnumValue<T>.ToString() returns
                // "TotalsRowFunctionValues { }" — use InnerText for the
                // OOXML-canonical lowercase token. CONSISTENCY(enum-innertext).

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Enumerate via the /table bare lister (lists every table as /Sheet/table[N]) and use a valid per-sheet N.
  2. Use a 1-based index in [1, perSheetTableCount].
  3. try/catch(ArgumentException) and read the (1..N) range from the message.

Example fix

// before
var col = handler.Get("/Sheet1/table[3]/columns[1]"); // throws if <3 tables

// after
var sheetTables = handler.Get("/table").Children
    .Where(c => c.Path.StartsWith("/Sheet1/table[", StringComparison.OrdinalIgnoreCase))
    .ToList();
if (sheetTables.Count == 0) return null;
var col = handler.Get(sheetTables[0].Path + "/columns[1]");
Defensive patterns

Strategy: validation

Validate before calling

// the /table bare lister enumerates every table as /Sheet/table[N]
var sheetTables = handler.Get("/table").Children
    .Where(c => c.Path.StartsWith($"/{sheet}/table[", StringComparison.OrdinalIgnoreCase))
    .ToList();
if (tIdx < 1 || tIdx > sheetTables.Count) return null;
return handler.Get($"/{sheet}/table[{tIdx}]/columns[{cIdx}]");

Type guard

static int? ElementIndex(string cellRef, string element)
{
    var m = Regex.Match(cellRef, $@"^{Regex.Escape(element)}\[(\d+)$", RegexOptions.IgnoreCase);
    return m.Success && int.TryParse(m.Groups[1].Value, out var i) ? i : null;
}

Prevention

When it happens

Trigger: handler.Get("/Sheet1/table[3]/columns[1]") on a sheet with fewer than 3 tables. table[0]. Using a workbook-wide table index where a per-sheet index is required.

Common situations: Hard-coded table index after tables were removed. Wrong sheet. Zero-based indexing. Reusing an index counted across the whole workbook.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/42c6da58ce08a587. Report an issue: GitHub.