iOfficeAI/OfficeCLI · error · ArgumentException

Chart {chartIdx} not found (total: {excelCharts.Count})

Error message

Chart {chartIdx} not found (total: {excelCharts.Count})

What it means

Thrown by AddChartSeries when the chart index parsed from the path is outside the valid range. The index is 1-based and must satisfy 1 <= chartIdx <= excelCharts.Count. The error message includes both the requested index and the total count so the user can see how far off they are.

Source

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

    // 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.
        string? valuesRef = null, categoriesRef = null;
        List<string>? cachedCats = null;
        if (properties.TryGetValue("values", out var valRaw) && ChartHelper.IsRangeReference(valRaw))
        {
            valuesRef = ChartHelper.NormalizeRangeReference(valRaw, sheetName);

View on GitHub (pinned to 1ced45e900)

Solutions

  1. List the charts on the sheet to find the correct 1-based index.
  2. Remember chart indices start at 1, not 0.
  3. If charts were deleted, re-enumerate to get the current index.
  4. Use the return path from a prior chart-creation add call (it includes the correct index).

Example fix

// before (0-based assumption, or sheet has only 1 chart)
add /Sheet1/chart[0] --type chart-series --data "S2:5,6,7"
// after
add /Sheet1/chart[1] --type chart-series --data "S2:5,6,7"
Defensive patterns

Strategy: validation

Validate before calling

// Verify the chart index is in range before calling AddChartSeries
var drawingsPart = worksheet.DrawingsPart;
if (drawingsPart != null)
{
    var charts = handler.GetExcelCharts(drawingsPart);
    if (chartIdx < 1 || chartIdx > charts.Count)
        throw new InvalidOperationException(
            $"Chart {chartIdx} not found (total charts on sheet: {charts.Count}).");
}

Try / catch

try { handler.AddChartSeries(parentPath, properties); }
catch (ArgumentException ex) when (ex.Message.Contains("not found (total:"))
{
    Console.Error.WriteLine($"{ex.Message} Use a 1-based index within range.");
}

Prevention

When it happens

Trigger: Calling add /Sheet1/chart[5] --type chart-series when the sheet has fewer than 5 charts. Also fires with chart[0] (indices are 1-based) or chart[-1] (though the regex only allows digits, so 0 is the practical lower-bound mistake).

Common situations: User assumes a 0-based index. User hardcodes an index that was valid before charts were deleted or reordered. User miscounts after bulk chart creation.

Related errors


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