iOfficeAI/OfficeCLI · error · ArgumentException

Chart index {chartIdx} out of range (1-{allCharts.Count})

Error message

Chart index {chartIdx} out of range (1-{allCharts.Count})

What it means

Thrown for /Sheet/chart[N] when N exceeds the number of charts on the sheet (1-based). The chart count comes from GetExcelCharts over the sheet's DrawingsPart; the message states the valid (1-N) range. Distinct from 768 (no DrawingsPart at all).

Source

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

                throw new ArgumentException($"Axis not available on chart {caChartIdx}: extended charts not supported.");
            var axisNode = ChartHelper.BuildAxisNode(caChartInfo.StandardPart.ChartSpace, caRole, path);
            if (axisNode == null)
                throw new ArgumentException($"Axis with role '{caRole}' not found on chart {caChartIdx}.");
            return axisNode;
        }

        // Chart path: /Sheet1/chart[N] or /Sheet1/chart[N]/series[K]
        var chartMatch = Regex.Match(cellRef, @"^chart\[(\d+)\](?:/series\[(\d+)\])?$");
        if (chartMatch.Success)
        {
            var chartIdx = int.Parse(chartMatch.Groups[1].Value);
            var drawingsPart = worksheet.DrawingsPart;
            if (drawingsPart == null)
                throw new ArgumentException($"No charts found in sheet");

            var allCharts = GetExcelCharts(drawingsPart);
            if (chartIdx < 1 || chartIdx > allCharts.Count)
                throw new ArgumentException($"Chart index {chartIdx} out of range (1-{allCharts.Count})");

            var chartInfo = allCharts[chartIdx - 1];
            var chartNode = new DocumentNode { Path = $"/{sheetNameFromPath}/chart[{chartIdx}]", Type = "chart" };

            // BUG-R11-04: chart Get used to skip the TwoCellAnchor even though
            // `add chart --prop anchor=B2:F7` and `set ... anchor=...` both
            // support it. Round-trip requires Get to surface the anchor range
            // in the same `B2:F7` grammar. CONSISTENCY(ole-width-units) —
            // mirrors the Add/Set accepted grammar.
            var chartAnchorRange = GetChartAnchorRange(drawingsPart, chartIdx);
            if (chartAnchorRange != null)
                chartNode.Format["anchor"] = chartAnchorRange;

            // CONSISTENCY(ole-width-units): also surface x/y/width/height in cm,
            // matching the schema's add/set vocabulary so round-trip works.
            PopulateChartPositionFormat(drawingsPart, chartIdx, chartNode);

            if (chartInfo.IsExtended)

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Use a 1-based index in [1, chartCount].
  2. Enumerate the charts first to learn the count, then index.
  3. try/catch(ArgumentException) and parse the (1-N) range from the message.

Example fix

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

// after
try { var chart = handler.Get("/Sheet1/chart[5]"); }
catch (ArgumentException) { /* chart index invalid; pick a valid N */ }
Defensive patterns

Strategy: try-catch

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[5]"); }
catch (ArgumentException ex) { /* ex.Message carries the valid (1-N) chart range */ return null; }

Prevention

When it happens

Trigger: handler.Get("/Sheet1/chart[5]") on a sheet with fewer than 5 charts. chart[0] (indices are 1-based).

Common situations: Hard-coded chart index after charts were added or removed. Zero-based indexing. Looping charts with an off-by-one upper bound.

Related errors


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