iOfficeAI/OfficeCLI · error · ArgumentException

Sheet not found

Error message

Sheet not found: {picSheetName}

What it means

Thrown by AddPicture when the sheet name extracted from the parentPath does not resolve to an existing worksheet. The path is split on the first '/' and the first segment is treated as the sheet name; if FindWorksheet returns null, the error fires before any image-path validation.

Solutions

  1. List worksheets to get the exact sheet name.
  2. Check for casing and trailing-space issues.
  3. Create the sheet first if it should exist.

Example fix

// before (sheet is 'Cover')
add /cover --type picture --src logo.png
// after
add /Cover --type picture --src logo.png
Defensive patterns

Strategy: validation

Validate before calling

// Check the sheet exists before calling AddPicture
var sheetName = parentPath.TrimStart('/').Split('/', 2)[0];
if (handler.FindWorksheet(sheetName) == null)
    throw new InvalidOperationException($"Sheet not found: {sheetName}");

Try / catch

try { handler.AddPicture(parentPath, type, position, properties); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Sheet not found:"))
{
    Console.Error.WriteLine($"{ex.Message} Verify the sheet name.");
}

Prevention

When it happens

Trigger: Calling add /NonExistentSheet --type picture --src logo.png. The worksheet with that name does not exist in the workbook.

Common situations: Sheet renamed or deleted. Case or whitespace mismatch. Script targets a sheet from a different workbook.

Related errors


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

Appendix: source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Add.Drawings.cs:270

                oleWsElement.InsertBefore(oleObjects, insertBefore);
            else
                oleWsElement.AppendChild(oleObjects);
        }
        oleObjects.AppendChild(oleObj);

        SaveWorksheet(oleWorksheet);

        var oleCount = oleWsElement.Descendants<OleObject>().Count();
        return $"/{oleSheetName}/ole[{oleCount}]";
    }

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

        if (!properties.TryGetValue("path", out var imgPath)
            && !properties.TryGetValue("src", out imgPath))
            throw new ArgumentException("'src' property is required for picture type");

        // CONSISTENCY(picture-emu): use ParseAnchorBoundsEmu like OLE,
        // so width/height accept unit-qualified strings ("6cm", "2in")
        // in addition to bare integer cell counts.
        var (px, py, pwEmu, phEmu) = ParseAnchorBoundsEmu(properties, "0", "0", "5", "5");
        // P9: accept `altText=` as alias for `alt=`.
        // CONSISTENCY(picture-alt): description completes the shared alias set.
        var alt = properties.GetValueOrDefault("alt")
            ?? properties.GetValueOrDefault("altText")
            ?? properties.GetValueOrDefault("alttext")
            ?? properties.GetValueOrDefault("description", "");

        // Resolve the image bytes AND parse/validate the anchor BEFORE any
        // part is created: a bad data URI or anchor used to fail after the

View on GitHub (pinned to 1ced45e900)