{"record":{"id":"55b357f418175be6","repo":"iOfficeAI/OfficeCLI","slug":"chart-chartidx-not-found-total-excelcharts-co","errorCode":null,"errorMessage":"Chart {chartIdx} not found (total: {excelCharts.Count})","messagePattern":"Chart (.+?) not found \\(total: (.+?)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.Chart.cs","lineNumber":388,"sourceCode":"    // series to an existing chart. Mirrors PowerPointHandler.AddChartSeries\n    // (R22-1); additionally resolves xlsx cell-range values/categories into\n    // numRef/strRef + cached snapshot, matching what chart Add emits for\n    // range-referenced series (CONSISTENCY(chart-series-rangeref-cache)).\n    private string AddChartSeries(string parentPath, Dictionary<string, string> properties)\n    {\n        var m = Regex.Match(parentPath, @\"^/([^/]+)/chart\\[(\\d+)\\]$\");\n        if (!m.Success)\n            throw new ArgumentException(\n                \"series must be added to a chart parent: /SheetName/chart[N]\");\n        var sheetName = m.Groups[1].Value;\n        var chartIdx = int.Parse(m.Groups[2].Value);\n        var worksheet = FindWorksheet(sheetName)\n            ?? throw new ArgumentException($\"Sheet not found: {sheetName}\");\n        var drawingsPart = worksheet.DrawingsPart\n            ?? throw new ArgumentException(\"Sheet has no drawings/charts\");\n        var excelCharts = GetExcelCharts(drawingsPart);\n        if (chartIdx < 1 || chartIdx > excelCharts.Count)\n            throw new ArgumentException($\"Chart {chartIdx} not found (total: {excelCharts.Count})\");\n        var chartInfo = excelCharts[chartIdx - 1];\n        if (chartInfo.StandardPart == null)\n            throw new ArgumentException(\n                $\"Chart at {parentPath} is not a standard chart (extended cx charts do not support add series).\");\n        var chartPart = chartInfo.StandardPart;\n\n        // Resolve range-reference values/categories against the workbook so\n        // AddSeries seeds literal data (which becomes the cached snapshot).\n        // Mutate `properties` in place instead of copying: enumerating a\n        // TrackingPropertyDictionary into a fresh dict marks EVERY key as\n        // consumed (see TrackingPropertyDictionary.GetEnumerator), which\n        // silently swallows unsupported_property reporting for unknown keys.\n        // TryGetValue / indexer / Remove are the tracked access routes.\n        string? valuesRef = null, categoriesRef = null;\n        List<string>? cachedCats = null;\n        if (properties.TryGetValue(\"values\", out var valRaw) && ChartHelper.IsRangeReference(valRaw))\n        {\n            valuesRef = ChartHelper.NormalizeRangeReference(valRaw, sheetName);","sourceCodeStart":370,"sourceCodeEnd":406,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.Chart.cs#L370-L406","documentation":"Thrown by AddChartSeries when the chart index parsed from the path is outside the valid range. The index is 1-based and must satisfy 1 <= chartIdx <= excelCharts.Count. The error message includes both the requested index and the total count so the user can see how far off they are.","triggerScenarios":"Calling add /Sheet1/chart[5] --type chart-series when the sheet has fewer than 5 charts. Also fires with chart[0] (indices are 1-based) or chart[-1] (though the regex only allows digits, so 0 is the practical lower-bound mistake).","commonSituations":"User assumes a 0-based index. User hardcodes an index that was valid before charts were deleted or reordered. User miscounts after bulk chart creation.","solutions":["List the charts on the sheet to find the correct 1-based index.","Remember chart indices start at 1, not 0.","If charts were deleted, re-enumerate to get the current index.","Use the return path from a prior chart-creation add call (it includes the correct index)."],"exampleFix":"// before (0-based assumption, or sheet has only 1 chart)\nadd /Sheet1/chart[0] --type chart-series --data \"S2:5,6,7\"\n// after\nadd /Sheet1/chart[1] --type chart-series --data \"S2:5,6,7\"","handlingStrategy":"validation","validationCode":"// Verify the chart index is in range before calling AddChartSeries\nvar drawingsPart = worksheet.DrawingsPart;\nif (drawingsPart != null)\n{\n    var charts = handler.GetExcelCharts(drawingsPart);\n    if (chartIdx < 1 || chartIdx > charts.Count)\n        throw new InvalidOperationException(\n            $\"Chart {chartIdx} not found (total charts on sheet: {charts.Count}).\");\n}","typeGuard":null,"tryCatchPattern":"try { handler.AddChartSeries(parentPath, properties); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"not found (total:\"))\n{\n    Console.Error.WriteLine($\"{ex.Message} Use a 1-based index within range.\");\n}","preventionTips":["List charts on the sheet to find the correct 1-based index.","Never assume a 0-based index for chart paths.","After deleting charts, re-enumerate remaining indices.","Reuse the chart index from the creation call's return path."],"tags":["excel","chart","series","index-out-of-range","validation"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}