{"record":{"id":"a4a726c714769ca2","repo":"iOfficeAI/OfficeCLI","slug":"chart-index-chartidx-out-of-range-1-allcharts","errorCode":null,"errorMessage":"Chart index {chartIdx} out of range (1-{allCharts.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":697,"sourceCode":"                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\n            var allCharts = GetExcelCharts(drawingsPart);\n            if (chartIdx < 1 || chartIdx > allCharts.Count)\n                throw new ArgumentException($\"Chart index {chartIdx} out of range (1-{allCharts.Count})\");\n\n            var chartInfo = allCharts[chartIdx - 1];\n            var chartNode = new DocumentNode { Path = $\"/{sheetNameFromPath}/chart[{chartIdx}]\", Type = \"chart\" };\n\n            // BUG-R11-04: chart Get used to skip the TwoCellAnchor even though\n            // `add chart --prop anchor=B2:F7` and `set ... anchor=...` both\n            // support it. Round-trip requires Get to surface the anchor range\n            // in the same `B2:F7` grammar. CONSISTENCY(ole-width-units) —\n            // mirrors the Add/Set accepted grammar.\n            var chartAnchorRange = GetChartAnchorRange(drawingsPart, chartIdx);\n            if (chartAnchorRange != null)\n                chartNode.Format[\"anchor\"] = chartAnchorRange;\n\n            // CONSISTENCY(ole-width-units): also surface x/y/width/height in cm,\n            // matching the schema's add/set vocabulary so round-trip works.\n            PopulateChartPositionFormat(drawingsPart, chartIdx, chartNode);\n\n            if (chartInfo.IsExtended)","sourceCodeStart":679,"sourceCodeEnd":715,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Query.cs#L679-L715","documentation":"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).","triggerScenarios":"handler.Get(\"/Sheet1/chart[5]\") 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 chart = handler.Get(\"/Sheet1/chart[5]\"); // throws if <5 charts\n\n// after\ntry { var chart = handler.Get(\"/Sheet1/chart[5]\"); }\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]\"); }\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","path-index","argument-exception"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}