iOfficeAI/OfficeCLI · error · ArgumentException

Sheet not found: {fbSheetName}

Error message

Sheet not found: {fbSheetName}

What it means

Thrown by the AddDefault fallback method when the sheet name extracted from the parentPath does not resolve to an existing worksheet. The path is split on the first '/' after trimming the leading slash; the first segment is the sheet name. This is the generic-add counterpart to the chart-series sheet-not-found check.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Add.Chart.cs:441

        var newIdx = ChartHelper.AddSeries(chartPart, properties);
        if (newIdx == 0)
            throw new ArgumentException(
                "Cannot add a series: the chart has no existing series to derive structure from. Recreate the chart with the desired series instead.");
        ChartHelper.ApplySeriesRangeRefs(chartPart, newIdx, valuesRef, categoriesRef, cachedCats);
        return $"/{sheetName}/chart[{chartIdx}]/series[{newIdx}]";
    }

    private string AddDefault(string parentPath, string type, InsertPosition? position, Dictionary<string, string> properties)
    {
        var index = position?.Index;
        // Generic fallback: create typed element via SDK schema validation
        // Parse parentPath: /<SheetName>/xmlPath...
        var fbSegments = parentPath.TrimStart('/').Split('/', 2);
        var fbSheetName = fbSegments[0];
        var fbWorksheet = FindWorksheet(fbSheetName);
        if (fbWorksheet == null)
            throw new ArgumentException($"Sheet not found: {fbSheetName}");

        OpenXmlElement fbParent = GetSheet(fbWorksheet);
        if (fbSegments.Length > 1 && !string.IsNullOrEmpty(fbSegments[1]))
        {
            var xmlSegments = GenericXmlQuery.ParsePathSegments(fbSegments[1]);
            fbParent = GenericXmlQuery.NavigateByPath(fbParent!, xmlSegments)
                ?? throw new ArgumentException($"Parent element not found: {parentPath}");
        }

        var created = GenericXmlQuery.TryCreateTypedElement(fbParent!, type, properties, index);
        if (created == null)
            throw new ArgumentException(
                $"Unknown element type '{type}' for {parentPath}. " +
                "Valid types: sheet, row, cell, shape, chart, ole (object, embed), autofilter, databar, colorscale, iconset, formulacf, comment, namedrange, table, picture, validation, pivottable. " +
                "Use 'officecli xlsx add' for details.");

        SaveWorksheet(fbWorksheet);

View on GitHub (pinned to 1ced45e900)

Solutions

  1. List worksheets to get the exact sheet name and update the path.
  2. Check for trailing spaces or case mismatches.
  3. Create the sheet first if it should exist.
  4. Verify the correct workbook file is being edited.

Example fix

// before (sheet is named 'Summary')
add /summry/A1 --type cell --value "hello"
// after
add /Summary/A1 --type cell --value "hello"
Defensive patterns

Strategy: validation

Validate before calling

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

Try / catch

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

Prevention

When it happens

Trigger: Calling add /NonExistentSheet/someElement --type cell or any generic add where the first path segment names a sheet that does not exist in the workbook.

Common situations: Sheet was renamed or deleted. Typo or casing mismatch in the sheet name. Script runs against a different workbook than expected.

Related errors


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