iOfficeAI/OfficeCLI · error · ArgumentException

Series {seriesIdx} not found (total: {seriesChildren.Count})

Error message

Series {seriesIdx} not found (total: {seriesChildren.Count})

What it means

Thrown for /Sheet/chart[N]/series[K] when K exceeds the number of series Children on chart N (1-based). Series Children are only populated for standard charts; an extended chart has zero series Children, so any series index throws 'Series K not found (total: 0)'. The count comes from chartNode.Children of Type 'series'.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Query.cs:741

                if (cxTitleText != null) chartNode.Format["title"] = cxTitleText;
                // Count series
                var cxSeries = cxChartSpace.Descendants<DocumentFormat.OpenXml.Office2016.Drawing.ChartDrawing.Series>().ToList();
                chartNode.Format["seriesCount"] = cxSeries.Count;
            }
            else
            {
                var chart = chartInfo.StandardPart!.ChartSpace?.GetFirstChild<DocumentFormat.OpenXml.Drawing.Charts.Chart>();
                if (chart != null)
                    ChartHelper.ReadChartProperties(chart, chartNode, chartMatch.Groups[2].Success ? 1 : depth);
            }

            // If series sub-path requested, extract the specific series child
            if (chartMatch.Groups[2].Success)
            {
                var seriesIdx = int.Parse(chartMatch.Groups[2].Value);
                var seriesChildren = chartNode.Children.Where(c => c.Type == "series").ToList();
                if (seriesIdx < 1 || seriesIdx > seriesChildren.Count)
                    throw new ArgumentException($"Series {seriesIdx} not found (total: {seriesChildren.Count})");
                var seriesNode = seriesChildren[seriesIdx - 1];
                seriesNode.Path = path;
                return seriesNode;
            }
            return chartNode;
        }

        // Pivot table path: /Sheet1/pivottable[N]
        var pivotMatch = Regex.Match(cellRef, @"^pivottable\[(\d+)\]$", RegexOptions.IgnoreCase);
        if (pivotMatch.Success)
        {
            var ptIdx = int.Parse(pivotMatch.Groups[1].Value);
            var pivotParts = worksheet.PivotTableParts.ToList();
            if (ptIdx < 1 || ptIdx > pivotParts.Count)
                throw new ArgumentException($"PivotTable index {ptIdx} out of range (1-{pivotParts.Count})");

            var pivotPart = pivotParts[ptIdx - 1];
            var ptNode = new DocumentNode { Path = path, Type = "pivottable" };

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Use a 1-based index within the chart's series-children count.
  2. For extended charts, read seriesCount from Format instead of indexing series Children.
  3. Enumerate the chart node's series Children first to learn the count.

Example fix

// before
var s = handler.Get("/Sheet1/chart[1]/series[5]"); // throws if <5 series

// after
var chart = handler.Get("/Sheet1/chart[1]");
var series = chart.Children.Where(c => c.Type == "series").ToList();
if (series.Count == 0) return null; // extended chart — read chart.Format["seriesCount"] instead
var s = handler.Get("/Sheet1/chart[1]/series[" + Math.Min(5, series.Count) + "]");
Defensive patterns

Strategy: try-catch

Validate before calling

// discover series count from the chart node, then index safely
var chart = handler.Get("/Sheet1/chart[1]");
var series = chart.Children.Where(c => c.Type == "series").ToList();
if (series.Count == 0) return null; // extended chart — read Format["seriesCount"]
var n = Math.Clamp(k, 1, series.Count);
return handler.Get($"/Sheet1/chart[1]/series[{n}]");

Type guard

static int? ElementIndex(string cellRef, string element)
{
    var m = Regex.Match(cellRef, $@"^{Regex.Escape(element)}\[(\d+)$", RegexOptions.IgnoreCase);
    return m.Success && int.TryParse(m.Groups[1].Value, out var i) ? i : null;
}

Try / catch

try { return handler.Get("/Sheet1/chart[1]/series[5]"); }
catch (ArgumentException ex) { /* ex.Message: 'Series 5 not found (total: N)' */ return null; }

Prevention

When it happens

Trigger: handler.Get("/Sheet1/chart[1]/series[5]") on a chart with fewer than 5 series. Indexing a series on an extended chart (total: 0). series[0].

Common situations: Hard-coded series index after series were added or removed. Querying series on a modern/extended chart. Zero-based indexing.

Related errors


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