{"record":{"id":"60722fad334a42c0","repo":"iOfficeAI/OfficeCLI","slug":"chart-index-cachartidx-out-of-range-1-caallcha","errorCode":null,"errorMessage":"Chart index {caChartIdx} out of range (1-{caAllCharts.Count})","messagePattern":"Chart index (.+?) out of range \\(1-(.+?)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Query.cs","lineNumber":676,"sourceCode":"            if (af?.Reference?.Value != null) afNode.Format[\"range\"] = af.Reference.Value;\n            if (af != null) PopulateAutoFilterCriteria(af, afNode);\n            return afNode;\n        }\n\n        // Chart axis-by-role sub-path: /Sheet1/chart[N]/axis[@role=ROLE].\n        // Per schemas/help/pptx/chart-axis.json (shared contract).\n        var chartAxisGetMatch = Regex.Match(cellRef,\n            @\"^chart\\[(\\d+)\\]/axis\\[@role=([a-zA-Z0-9_]+)\\]$\");\n        if (chartAxisGetMatch.Success)\n        {\n            var caChartIdx = int.Parse(chartAxisGetMatch.Groups[1].Value);\n            var caRole = chartAxisGetMatch.Groups[2].Value;\n            var caDrawingsPart = worksheet.DrawingsPart;\n            if (caDrawingsPart == null)\n                throw new ArgumentException($\"No charts found in sheet\");\n            var caAllCharts = GetExcelCharts(caDrawingsPart);\n            if (caChartIdx < 1 || caChartIdx > caAllCharts.Count)\n                throw new ArgumentException($\"Chart index {caChartIdx} out of range (1-{caAllCharts.Count})\");\n            var caChartInfo = caAllCharts[caChartIdx - 1];\n            if (caChartInfo.IsExtended || caChartInfo.StandardPart?.ChartSpace == null)\n                throw new ArgumentException($\"Axis not available on chart {caChartIdx}: extended charts not supported.\");\n            var axisNode = ChartHelper.BuildAxisNode(caChartInfo.StandardPart.ChartSpace, caRole, path);\n            if (axisNode == null)\n                throw new ArgumentException($\"Axis with role '{caRole}' not found on chart {caChartIdx}.\");\n            return axisNode;\n        }\n\n        // Chart path: /Sheet1/chart[N] or /Sheet1/chart[N]/series[K]\n        var chartMatch = Regex.Match(cellRef, @\"^chart\\[(\\d+)\\](?:/series\\[(\\d+)\\])?$\");\n        if (chartMatch.Success)\n        {\n            var chartIdx = int.Parse(chartMatch.Groups[1].Value);\n            var drawingsPart = worksheet.DrawingsPart;\n            if (drawingsPart == null)\n                throw new ArgumentException($\"No charts found in sheet\");\n","sourceCodeStart":658,"sourceCodeEnd":694,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Query.cs#L658-L694","documentation":"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.","triggerScenarios":"handler.Get(\"/Sheet1/chart[5]/axis[@role=primary]\") on a sheet with fewer than 5 charts. chart[0] (indices are 1-based).","commonSituations":"Hard-coded chart index after charts were added or removed. Zero-based indexing. Looping charts with an off-by-one upper bound.","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."],"exampleFix":"// before\nvar axis = handler.Get(\"/Sheet1/chart[5]/axis[@role=primary]\"); // throws if <5 charts\n\n// after\ntry { var axis = handler.Get(\"/Sheet1/chart[5]/axis[@role=primary]\"); }\ncatch (ArgumentException) { /* chart index invalid; pick a valid N */ }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"static int? ElementIndex(string cellRef, string element)\n{\n    var m = Regex.Match(cellRef, $@\"^{Regex.Escape(element)}\\[(\\d+)$\", RegexOptions.IgnoreCase);\n    return m.Success && int.TryParse(m.Groups[1].Value, out var i) ? i : null;\n}","tryCatchPattern":"try { return handler.Get(\"/Sheet1/chart[5]/axis[@role=primary]\"); }\ncatch (ArgumentException ex) { /* ex.Message carries the valid (1-N) chart range */ return null; }","preventionTips":["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."],"tags":["excel","charts","axis","path-index","argument-exception"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}