iOfficeAI/OfficeCLI · error · ArgumentException

Sheet not found: {sheetName}

Error message

Sheet not found: {sheetName}

What it means

Thrown by ExcelHandler.Import when the sheet name parsed from the parentPath does not resolve to any worksheet via FindWorksheet. The parentPath's first segment (after stripping leading '/') is treated as the sheet name; a missing or misspelled name fails here before any CSV parsing begins.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Import.cs:28

public partial class ExcelHandler
{
    /// <summary>
    /// Import CSV/TSV data into a worksheet starting at the given cell.
    /// </summary>
    /// <param name="parentPath">Sheet path, e.g. "/Sheet1"</param>
    /// <param name="csvContent">Raw CSV/TSV string content</param>
    /// <param name="delimiter">Field delimiter: ',' for CSV, '\t' for TSV</param>
    /// <param name="hasHeader">If true, set AutoFilter and freeze pane on first row</param>
    /// <param name="startCell">Starting cell reference, e.g. "A1"</param>
    /// <returns>Summary of rows/cols imported</returns>
    public string Import(string parentPath, string csvContent, char delimiter, bool hasHeader, string startCell)
    {
        parentPath = NormalizeExcelPath(parentPath);
        parentPath = ResolveSheetIndexInPath(parentPath);
        var sheetName = parentPath.TrimStart('/').Split('/', 2)[0];
        var worksheet = FindWorksheet(sheetName)
            ?? throw new ArgumentException($"Sheet not found: {sheetName}");

        var ws = GetSheet(worksheet);
        var sheetData = ws.GetFirstChild<SheetData>()
            ?? ws.AppendChild(new SheetData());

        // Parse start cell
        var (startCol, startRow) = ParseCellReference(startCell.ToUpperInvariant());
        var startColIdx = ColumnNameToIndex(startCol);

        // Parse CSV
        var rows = ParseCsv(csvContent, delimiter);
        if (rows.Count == 0)
            return "No data to import";

        int maxCols = 0;
        for (int r = 0; r < rows.Count; r++)
            if (rows[r].Count > maxCols) maxCols = rows[r].Count;

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Verify the sheet exists first via Get / (it lists all sheets) and use the exact name returned.
  2. Create the sheet before importing (add /NewSheet --type sheet), then import into /NewSheet.
  3. If using sheet[N] index syntax, confirm the index is in range; prefer the explicit sheet name.
  4. Check for accidental leading/trailing whitespace or quotes in the parentPath.

Example fix

// before
excel.Import("/SalesData", csv, ',', true, "A1");   // sheet does not exist

// after
excel.Add("/SalesData", "sheet");
excel.Import("/SalesData", csv, ',', true, "A1");
Defensive patterns

Strategy: validation

Validate before calling

var sheetName = parentPath.TrimStart('/').Split('/', 2)[0];
if (excel.FindWorksheet(sheetName) == null)
    throw new ArgumentException($"Sheet '{sheetName}' does not exist. Create it first.");

Prevention

When it happens

Trigger: Calling Import with parentPath='/Nonexistent', '/Sheet1 ' (trailing space after normalization edge), a typo'd sheet name, or a path that resolves to a sheet index that does not exist (ResolveSheetIndexInPath returned a name FindWorksheet cannot match).

Common situations: Importing into a sheet that has not been created yet; case-sensitivity confusion after NormalizeExcelPath; reading the sheet name from a config field that was empty; targeting a hidden/very-deeply-named sheet by a stale label.

Related errors


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