iOfficeAI/OfficeCLI · warning · ArgumentException

Table {tableIndex} has no definition

Error message

Table {tableIndex} has no definition

What it means

Thrown by TableToNode when the TableDefinitionPart at the requested index has a null .Table property — the part exists in the package but its underlying definition element is missing. Indicates a malformed/corrupt OOXML package rather than a wrong index.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Helpers.Node.cs:1040

            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)
            node.Format["style"] = styleInfo.Name.Value;
        if (styleInfo != null)
        {

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Open the file in Excel and let it repair, then re-save — this restores the missing definition.
  2. Regenerate the file from the source of truth if it is programmatically produced.
  3. Inspect xl/worksheets/_rels/*.rels and the target tableN.xml to find the broken relationship.

Example fix

null
Defensive patterns

Strategy: try-catch

Validate before calling

// Cannot pre-validate without re-implementing the loader; treat as load-time risk
static bool TryGetTable(WorksheetPart wsp, int idx, out Table tbl)
{
    tbl = null;
    var parts = wsp.TableDefinitionParts.ToList();
    if (idx < 1 || idx > parts.Count) return false;
    tbl = parts[idx - 1].Table;
    return tbl != null;
}

Type guard

null

Try / catch

try { TableToNode(...); }
catch (ArgumentException ex) when (ex.Message.Contains("has no definition"))
{ // log corrupt-file path; do not retry on the same file
  File.Copy(path, path + ".corrupt", overwrite: true); }

Prevention

When it happens

Trigger: Loading an .xlsx where a table part was left in the package but its table1.xml is empty or missing the <table> root; hand-edited packages that broke a relationship target; partial-write corruption.

Common situations: Files produced by a buggy generator that wrote the relationship but not the part content; zip manipulation that dropped a part after adding its rels; round-trip through a tool that strips tables.

Related errors


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