iOfficeAI/OfficeCLI · error · ArgumentException

Sheet not found: {tblSheetName}

Error message

Sheet not found: {tblSheetName}

What it means

Thrown by AddTable when the first segment of the parent path (the sheet name) does not resolve to an existing worksheet via FindWorksheet. A table must live on a concrete sheet, so an unknown sheet name cannot host the new table. The handler rejects it up front instead of creating an orphan part.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Add.Tables.cs:1064

                        : fop == FilterOperatorValues.NotEqual ? "notEquals"
                        : fop == FilterOperatorValues.GreaterThan ? "gt"
                        : fop == FilterOperatorValues.GreaterThanOrEqual ? "gte"
                        : fop == FilterOperatorValues.LessThan ? "lt"
                        : fop == FilterOperatorValues.LessThanOrEqual ? "lte"
                        : "equals";
                    node.Format[prefix + op] = val;
                }
            }
        }
    }

    private string AddTable(string parentPath, string type, InsertPosition? position, Dictionary<string, string> properties)
    {
        var index = position?.Index;
        var tblSegments = parentPath.TrimStart('/').Split('/', 2);
        var tblSheetName = tblSegments[0];
        var tblWorksheet = FindWorksheet(tblSheetName)
            ?? throw new ArgumentException($"Sheet not found: {tblSheetName}");

        var rangeRef = (properties.GetValueOrDefault("ref") ?? properties.GetValueOrDefault("range")
            ?? throw new ArgumentException("Property 'ref' or 'range' is required for table")).ToUpperInvariant();

        // T4 — reject a new table whose ref overlaps any existing table on
        // the same sheet. Excel silently corrupts the file otherwise.
        foreach (var existingTdp in tblWorksheet.TableDefinitionParts)
        {
            var existing = existingTdp.Table;
            if (existing?.Reference?.Value is not string existingRef) continue;
            if (RangesOverlap(rangeRef, existingRef))
                throw new ArgumentException(
                    $"Table ref overlaps existing table '{existing.Name?.Value ?? existing.DisplayName?.Value}' ({existingRef})");
        }


        var existingTableIds = _doc.WorkbookPart!.WorksheetParts
            .SelectMany(wp => wp.TableDefinitionParts)

View on GitHub (pinned to 1ced45e900)

Solutions

  1. List sheets first and copy the exact name (query the workbook's sheet list).
  2. Quote sheet names containing spaces exactly as they appear in the workbook.
  3. If you intended a new sheet, add it first with --type sheet, then add the table to it.

Example fix

// before
add /Q1Report/table --prop ref=A1:D10
// after (verify sheet exists; create if needed)
add /sheet --prop name=Q1Report
add /Q1Report/table --prop ref=A1:D10
Defensive patterns

Strategy: validation

Validate before calling

// Confirm the host sheet exists before adding a table to it.
var sheetName = parentPath.TrimStart('/').Split('/', 2)[0];
if (handler.FindWorksheet(sheetName) is null)
    throw new InvalidOperationException($"Cannot add table: sheet '{sheetName}' not found.");

Try / catch

try { handler.Add(parentPath, "table", null, props); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Sheet not found"))
{ /* list sheets and retry with the correct name */ }

Prevention

When it happens

Trigger: Calling Add('/MissingSheet/table', ...) or Add('/Sheet 1/table', ...) when no worksheet by that exact name exists, including whitespace, case, or renamed-sheet mismatches.

Common situations: Sheet was renamed or deleted between calls, a sheet name with spaces was not quoted correctly, or the caller used a 1-based index where a name was expected.

Related errors


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