iOfficeAI/OfficeCLI · error · ArgumentException

Sheet not found: {cellSheetName}

Error message

Sheet not found: {cellSheetName}

What it means

AddCell resolves the parent path's first segment as the worksheet name and looks it up via FindWorksheet. If no worksheet by that name exists (case-insensitive), it throws before touching SheetData. This prevents writing cells into a non-existent sheet, which would otherwise create an orphan <c> or fail silently.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Add.Cells.cs:333

        // by a prior cell op on the same sheet, it now lacks the new row
        // — a subsequent AddCell at the same row index would cache-miss
        // and create a duplicate <x:row r="N">, producing an
        // Excel-rejected file. Invalidate the cache to force a rescan.
        InvalidateRowIndex(sheetData);

        if (needsShift)
            DeleteCalcChainIfPresent();
        SaveWorksheet(worksheet);
        return $"/{sheetName}/row[{rowIdx}]";
    }

    private string AddCell(string parentPath, string type, InsertPosition? position, Dictionary<string, string> properties)
    {
        var index = position?.Index;
        var cellSegments = parentPath.TrimStart('/').Split('/', 2);
        var cellSheetName = cellSegments[0];
        var cellWorksheet = FindWorksheet(cellSheetName)
            ?? throw new ArgumentException($"Sheet not found: {cellSheetName}");
        var cellSheetData = GetSheet(cellWorksheet).GetFirstChild<SheetData>()
            ?? GetSheet(cellWorksheet).AppendChild(new SheetData());

        // R7-1: if path tail is a cell-ref (e.g. /Sheet1/Z99), treat it
        // as the target address — equivalent to --prop ref=Z99. Parity
        // with the `comment` case below which already does this.
        string? cellRefFromPath = null;
        if (cellSegments.Length > 1 && Regex.IsMatch(cellSegments[1], @"^[A-Z]+\d+$", RegexOptions.IgnoreCase))
            cellRefFromPath = cellSegments[1].ToUpperInvariant();
        // R10-2: also honor a cell[<ref>] path tail (e.g. /Sheet1/cell[C5]) so
        // `add /Sheet1/cell[C5] cell` lands at C5 instead of silently snapping
        // to A1. Mirrors the bare-cellref tail above and the row[N] tail below;
        // without it, "cell[C5]" matched neither regex and auto-assign chose A1.
        else if (cellSegments.Length > 1)
        {
            var cellPathMatch = Regex.Match(cellSegments[1], @"^cell\[([A-Z]+\d+)\]$", RegexOptions.IgnoreCase);
            if (cellPathMatch.Success)
                cellRefFromPath = cellPathMatch.Groups[1].Value.ToUpperInvariant();

View on GitHub (pinned to 1ced45e900)

Solutions

  1. List sheets first (Get/Query on the workbook root) and confirm the exact name exists before adding cells.
  2. If the sheet should exist, create it with Add("/","sheet",null,{["name"]=sheetName}) before adding cells.
  3. Check for typos and stray whitespace in the sheet-name segment of parentPath.

Example fix

// before
handler.Add("/Shet1/A1", "cell", null, new() { ["value"] = "x" });
// after — ensure the sheet exists first
EnsureSheet(handler, "Sheet1");
handler.Add("/Sheet1/A1", "cell", null, new() { ["value"] = "x" });
Defensive patterns

Strategy: validation

Validate before calling

string sheet = cellPath.TrimStart('/').Split('/', 2)[0];
if (h.Query("/").All(n => !n.Name.Equals(sheet, StringComparison.OrdinalIgnoreCase)))
    throw new ArgumentException($"Sheet '{sheet}' does not exist.");
h.Add(cellPath, "cell", pos, props);

Try / catch

try { h.Add(cellPath, "cell", pos, props); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Sheet not found"))
{ /* create the sheet or correct the name */ }

Prevention

When it happens

Trigger: Add("/NonExistent/A1","cell",pos,props); Add("/Sheet1/A1","cell",...) on a workbook where the sheet was renamed/deleted; a typo in the sheet name; a sheet path using an index that ResolveSheetIndexInPath could not map.

Common situations: Hardcoded sheet names that drift after a rename; replaying a batch recorded against one workbook onto a different workbook whose sheets differ; case mismatch that looks identical but the sheet genuinely does not exist (note FindWorksheet is case-insensitive, so genuine case differences would NOT throw here).

Related errors


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