iOfficeAI/OfficeCLI · error · ArgumentException

Sheet not found: {sheetName}

Error message

Sheet not found: {sheetName}

What it means

Thrown by GetDumpRowNodes(sheetName): it builds the per-row DocumentNode list for the dump emitter, enumerating cells (including styled-empty cells the bulk Get path omits) and evaluating formulas with one FormulaEvaluator per sheet. FindWorksheet returns null when no worksheet matches the (case-insensitive) name.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.DumpSupport.cs:62

        if (!string.IsNullOrEmpty(pkgProps.Category)) node.Format["category"] = pkgProps.Category!;
        return node;
    }

    /// <summary>
    /// Enumerate every row of a sheet with ALL cells that carry content OR
    /// style. The bulk Get path (GetSheetChildNodes) intentionally omits
    /// styled-empty cells (&lt;c s="1"/&gt;, issue #149 bloat guard); a dump
    /// must include them because their xf holds user-visible formatting
    /// (filled header bands, bordered empty grids). Each cell node is built
    /// by the same CellToNode Get uses, so Format keys match Get exactly.
    /// A dump-only <c>__raw</c> Format key carries the raw stored
    /// &lt;x:v&gt; text so the emitter can reproduce numbers/dates without
    /// going through display formatting.
    /// </summary>
    public List<DocumentNode> GetDumpRowNodes(string sheetName)
    {
        var worksheet = FindWorksheet(sheetName)
            ?? throw new ArgumentException($"Sheet not found: {sheetName}");
        var rows = new List<DocumentNode>();
        var sheetData = GetSheet(worksheet).GetFirstChild<SheetData>();
        if (sheetData == null) return rows;

        // One evaluator per sheet: CellToNode lazily creates a fresh
        // FormulaEvaluator per formula cell when none is passed, which is
        // O(cells × sheet-size) on formula-heavy sheets.
        var eval = new Core.FormulaEvaluator(sheetData, _doc.WorkbookPart);
        // For unresolved-shared-string detection: a cell with t="s" whose
        // index has no entry (missing/truncated sharedStrings part) would
        // otherwise surface its INDEX as the cell text — confidently-wrong
        // data. Mark such cells so the emitter warns and skips them.
        var sstCount = _doc.WorkbookPart?.SharedStringTablePart?.SharedStringTable?
            .Elements<SharedStringItem>().Count() ?? 0;
        var seenRowIndices = new HashSet<uint>();
        foreach (var row in sheetData.Elements<Row>())
        {
            var ridx = row.RowIndex?.Value ?? 0;

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Resolve the sheet name through the dump's own token resolver before calling.
  2. Re-fetch the live sheet list and pass an exact name.
  3. Guard the dump loop to skip/renotify on missing sheets instead of throwing.

Example fix

// before
var nodes = handler.GetDumpRowNodes("SheetX");
// after — resolve against live sheets first
var live = handler.GetWorksheets().Select(w => w.Name).ToList();
var name = live.FirstOrDefault(n => n.Equals("SheetX", StringComparison.OrdinalIgnoreCase));
var nodes = name != null ? handler.GetDumpRowNodes(name) : new List<DocumentNode>();
Defensive patterns

Strategy: validation

Validate before calling

if (!handler.GetWorksheets().Any(w => w.Name.Equals(sheetName, StringComparison.OrdinalIgnoreCase)))
    /* skip or reconcile before GetDumpRowNodes */

Type guard

static bool SheetResolves(ExcelHandler h, string sheetName) =>
    h.GetWorksheets().Any(w => w.Name.Equals(sheetName, StringComparison.OrdinalIgnoreCase));

Try / catch

try { return handler.GetDumpRowNodes(sheetName); }
catch (ArgumentException ex) when (ex.Message.Contains("Sheet not found"))
{ return new List<DocumentNode>(); }

Prevention

When it happens

Trigger: Calling GetDumpRowNodes with a sheet name that does not exist — a typo, a sheet[N] token that did not resolve via the dump resolver, or a sheet removed after the dump started.

Common situations: Dump driver iterating a cached sheet list that went stale (sheet deleted mid-run); a hand-invoked dump with a wrong name; a sheet index that pointed past the end after reorder.

Related errors


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