iOfficeAI/OfficeCLI · error · ArgumentException
Chart index {caChartIdx} out of range (1-{caAllCharts.Count}
Error message
Chart index {caChartIdx} out of range (1-{caAllCharts.Count}) What it means
Thrown for /Sheet/chart[N]/axis[@role=R] 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 764 (no charts at all), which fires when DrawingsPart is null.
Source
Thrown at src/officecli/Handlers/Excel/ExcelHandler.Query.cs:676
if (af?.Reference?.Value != null) afNode.Format["range"] = af.Reference.Value;
if (af != null) PopulateAutoFilterCriteria(af, afNode);
return afNode;
}
// Chart axis-by-role sub-path: /Sheet1/chart[N]/axis[@role=ROLE].
// Per schemas/help/pptx/chart-axis.json (shared contract).
var chartAxisGetMatch = Regex.Match(cellRef,
@"^chart\[(\d+)\]/axis\[@role=([a-zA-Z0-9_]+)\]$");
if (chartAxisGetMatch.Success)
{
var caChartIdx = int.Parse(chartAxisGetMatch.Groups[1].Value);
var caRole = chartAxisGetMatch.Groups[2].Value;
var caDrawingsPart = worksheet.DrawingsPart;
if (caDrawingsPart == null)
throw new ArgumentException($"No charts found in sheet");
var caAllCharts = GetExcelCharts(caDrawingsPart);
if (caChartIdx < 1 || caChartIdx > caAllCharts.Count)
throw new ArgumentException($"Chart index {caChartIdx} out of range (1-{caAllCharts.Count})");
var caChartInfo = caAllCharts[caChartIdx - 1];
if (caChartInfo.IsExtended || caChartInfo.StandardPart?.ChartSpace == null)
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");
View on GitHub (pinned to 1ced45e900)
Solutions
- Use a 1-based index in [1, chartCount].
- Enumerate the charts first to learn the count, then index.
- try/catch(ArgumentException) and parse the (1-N) range from the message.
Example fix
// before
var axis = handler.Get("/Sheet1/chart[5]/axis[@role=primary]"); // throws if <5 charts
// after
try { var axis = handler.Get("/Sheet1/chart[5]/axis[@role=primary]"); }
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]/axis[@role=primary]"); }
catch (ArgumentException ex) { /* ex.Message carries the valid (1-N) chart range */ return null; } Prevention
- Chart indices are 1-based — there is no index 0.
- Enumerate the chart list first to learn the count, then index.
- Parse the (1-N) range from the exception message.
When it happens
Trigger: handler.Get("/Sheet1/chart[5]/axis[@role=primary]") 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
- No charts found in sheet
- Axis with role '{caRole}' not found on chart {caChartIdx}.
- Chart index {chartIdx} out of range (1-{allCharts.Count})
- Series {seriesIdx} not found (total: {seriesChildren.Count})
- Row break index {rbIdx} out of range (1-{breaks.Count})
AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13).
Data as JSON: /api/errors/60722fad334a42c0.
Report an issue: GitHub.