iOfficeAI/OfficeCLI · error · System.ArgumentException

Sheet not found: {runSheetName}

Error message

Sheet not found: {runSheetName}

What it means

Thrown by AddRun when FindWorksheet(runSheetName) returns null, meaning no worksheet in the workbook matches the sheet segment extracted from the parentPath. FindWorksheet performs a name lookup against the workbook's sheet collection. This fires after the path-format check (482) passes, so the path had two segments but the first did not resolve to a real sheet.

Source

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

            if (existingCol == null)
                columns.AppendChild(newCol);
        }

        SaveWorksheet(colWorksheet);
        return $"/{colSheetName}/col[{insertColName}]";
    }

    private string AddRun(string parentPath, string type, InsertPosition? position, Dictionary<string, string> properties)
    {
        var index = position?.Index;
        // Add a rich text run to a cell: parentPath = /SheetName/CellRef
        var runSegments = parentPath.TrimStart('/').Split('/', 2);
        if (runSegments.Length < 2)
            throw new ArgumentException("Parent path must be /SheetName/CellRef for adding a run");
        var runSheetName = runSegments[0];
        var runCellRef = runSegments[1].ToUpperInvariant();
        var runWorksheet = FindWorksheet(runSheetName)
            ?? throw new ArgumentException($"Sheet not found: {runSheetName}");
        var runSheetData = GetSheet(runWorksheet).GetFirstChild<SheetData>()
            ?? GetSheet(runWorksheet).AppendChild(new SheetData());
        var runCell = FindOrCreateCell(runSheetData, runCellRef);

        var runWbPart = _doc.WorkbookPart
            ?? throw new InvalidOperationException("Workbook not found");
        var runSstPart = runWbPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault()
            ?? runWbPart.AddNewPart<SharedStringTablePart>();
        SharedStringTable runSst;
        if (runSstPart.SharedStringTable != null)
            runSst = runSstPart.SharedStringTable;
        else
        {
            runSst = new SharedStringTable();
            runSstPart.SharedStringTable = runSst;
        }

        SharedStringItem? runSsi = null;

View on GitHub (pinned to 1ced45e900)

Solutions

  1. List sheets first (Get on /) and copy the exact sheet name into the path.
  2. Create the sheet with Add type=sheet before adding runs to its cells.
  3. Strip leading/trailing whitespace and verify the spelling of the sheet segment.

Example fix

// before
handler.Add("/Sheet1/A1", "run", pos, props); // Sheet1 does not exist

// after
handler.Add("/", "sheet", null, new() { ["name"] = "Sheet1" });
handler.Add("/Sheet1/A1", "run", pos, props);
Defensive patterns

Strategy: validation

Validate before calling

string sheet = "Sheet1";
// Verify via the library's read API before adding a run.
var sheets = handler.Query("/"); // or Get listing sheets
if (!sheets.Any(s => string.Equals(s.Name, sheet, StringComparison.OrdinalIgnoreCase)))
    throw new InvalidOperationException($"Sheet not found: {sheet}");
handler.Add("/" + sheet + "/A1", "run", pos, props);

Type guard

static bool SheetExists(IExcelHandler h, string sheet)
    => h.Query("/").Any(s => string.Equals(s.Name, sheet, StringComparison.OrdinalIgnoreCase));

Try / catch

try { handler.Add(parentPath, "run", pos, props); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Sheet not found"))
{ /* create sheet or report missing sheet to caller */ }

Prevention

When it happens

Trigger: AddRun with parentPath="/Nonexistent/A1". A typo in the sheet name, a sheet that was deleted, or a hidden/very-hidden sheet whose name differs. Case is handled by FindWorksheet's comparison in most paths, so this is a genuine name mismatch.

Common situations: Sheet was renamed or deleted between dump and add. Script uses a sheet name from a template that does not exist in the target workbook. Trailing whitespace or a copy-paste artifact in the sheet name.

Related errors


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