iOfficeAI/OfficeCLI · error · ArgumentException

Sheet not found: {sheetName}

Error message

Sheet not found: {sheetName}

What it means

Thrown by AddChartSeries when FindWorksheet returns null for the sheet name extracted from the parentPath. The sheet name is the first regex capture group from /SheetName/chart[N]; if no worksheet with that exact name exists in the workbook, this fires before any chart lookup is attempted.

Source

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

        var chartIdx = CountExcelCharts(drawingsPart);
        return $"/{chartSheetName}/chart[{chartIdx}]";
    }

    // BUG-002: `add /SheetName/chart[N] --type chart-series` — append a data
    // series to an existing chart. Mirrors PowerPointHandler.AddChartSeries
    // (R22-1); additionally resolves xlsx cell-range values/categories into
    // numRef/strRef + cached snapshot, matching what chart Add emits for
    // range-referenced series (CONSISTENCY(chart-series-rangeref-cache)).
    private string AddChartSeries(string parentPath, Dictionary<string, string> properties)
    {
        var m = Regex.Match(parentPath, @"^/([^/]+)/chart\[(\d+)\]$");
        if (!m.Success)
            throw new ArgumentException(
                "series must be added to a chart parent: /SheetName/chart[N]");
        var sheetName = m.Groups[1].Value;
        var chartIdx = int.Parse(m.Groups[2].Value);
        var worksheet = FindWorksheet(sheetName)
            ?? throw new ArgumentException($"Sheet not found: {sheetName}");
        var drawingsPart = worksheet.DrawingsPart
            ?? throw new ArgumentException("Sheet has no drawings/charts");
        var excelCharts = GetExcelCharts(drawingsPart);
        if (chartIdx < 1 || chartIdx > excelCharts.Count)
            throw new ArgumentException($"Chart {chartIdx} not found (total: {excelCharts.Count})");
        var chartInfo = excelCharts[chartIdx - 1];
        if (chartInfo.StandardPart == null)
            throw new ArgumentException(
                $"Chart at {parentPath} is not a standard chart (extended cx charts do not support add series).");
        var chartPart = chartInfo.StandardPart;

        // Resolve range-reference values/categories against the workbook so
        // AddSeries seeds literal data (which becomes the cached snapshot).
        // Mutate `properties` in place instead of copying: enumerating a
        // TrackingPropertyDictionary into a fresh dict marks EVERY key as
        // consumed (see TrackingPropertyDictionary.GetEnumerator), which
        // silently swallows unsupported_property reporting for unknown keys.
        // TryGetValue / indexer / Remove are the tracked access routes.

View on GitHub (pinned to 1ced45e900)

Solutions

  1. List the worksheets in the workbook first and copy the exact sheet name.
  2. Check for trailing/leading whitespace or casing differences in the sheet name.
  3. Rename the target sheet to match the path, or update the path to match the sheet.
  4. Ensure you are operating on the correct workbook file.

Example fix

// before (sheet is actually named 'Data')
add /data/chart[1] --type chart-series --data "S2:5,6,7"
// after
add /Data/chart[1] --type chart-series --data "S2:5,6,7"
Defensive patterns

Strategy: validation

Validate before calling

// Check the sheet exists before calling AddChartSeries
var match = System.Text.RegularExpressions.Regex.Match(parentPath, @"^/([^/]+)/chart\[(\d+)\]$");
if (match.Success)
{
    var sheetName = match.Groups[1].Value;
    if (handler.FindWorksheet(sheetName) == null)
        throw new InvalidOperationException($"Sheet not found: {sheetName}");
}

Try / catch

try { handler.AddChartSeries(parentPath, properties); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Sheet not found:"))
{
    Console.Error.WriteLine($"{ex.Message} List worksheets to find the correct name.");
}

Prevention

When it happens

Trigger: Calling add /NonExistentSheet/chart[1] --type chart-series. The sheet name is case-sensitive and must match an existing worksheet name exactly. Also fires if the sheet name has trailing spaces or was renamed after the path was constructed.

Common situations: Sheet was renamed or deleted between when the script was written and when it runs. Copy-paste introduces a trailing space or different casing. The path references a sheet from a different workbook.

Related errors


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