{"record":{"id":"ad7d06da46b1eb73","repo":"iOfficeAI/OfficeCLI","slug":"series-must-be-added-to-a-chart-parent-sheetname","errorCode":null,"errorMessage":"series must be added to a chart parent: /SheetName/chart[N]","messagePattern":"series must be added to a chart parent: /SheetName/chart\\[N\\]","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.Chart.cs","lineNumber":378,"sourceCode":"        drawingsPart.WorksheetDrawing.Append(anchor);\n        drawingsPart.WorksheetDrawing.Save();\n\n        // Legend is already handled inside BuildChartSpace\n\n        var chartIdx = CountExcelCharts(drawingsPart);\n        return $\"/{chartSheetName}/chart[{chartIdx}]\";\n    }\n\n    // BUG-002: `add /SheetName/chart[N] --type chart-series` — append a data\n    // 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).","sourceCodeStart":360,"sourceCodeEnd":396,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.Chart.cs#L360-L396","documentation":"Thrown by AddChartSeries when the parentPath does not match the required chart-parent pattern /SheetName/chart[N]. The regex ^/([^/]+)/chart\\[(\\d+)\\]$ requires a leading slash, a sheet name, the literal 'chart[', a 1-based index, and a closing bracket. This guards against appending a series to a non-chart or a malformed path.","triggerScenarios":"Calling add with --type chart-series on a parentPath like '/Sheet1', '/Sheet1/chart', '/Sheet1/chart1', '/Sheet1/shape[1]', or 'Sheet1/chart[1]' (missing leading slash). Only the exact pattern /<sheet>/chart[<number>] is accepted.","commonSituations":"User confuses the chart-series add path with a top-level add path. User writes chart index without brackets (chart1 instead of chart[1]). User targets a shape or picture path by mistake.","solutions":["Use the exact format /SheetName/chart[N] where N is the 1-based chart index, e.g. /Sheet1/chart[1].","First run a list/get to find the correct chart index for the target sheet.","If the parent is not a chart, remove --type chart-series and use the appropriate element type.","Ensure the leading slash and square brackets are present."],"exampleFix":"// before\nadd /Sheet1/chart1 --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":"// Validate chart-series parent path format before calling AddChartSeries\nvar pathRegex = new System.Text.RegularExpressions.Regex(@\"^/([^/]+)/chart\\[(\\d+)\\]$\");\nif (!pathRegex.IsMatch(parentPath))\n    throw new InvalidOperationException(\n        $\"Invalid chart-series parent path '{parentPath}'. Expected /SheetName/chart[N].\");","typeGuard":"static bool IsChartSeriesParentPath(string path) =>\n    System.Text.RegularExpressions.Regex.IsMatch(path, @\"^/([^/]+)/chart\\[(\\d+)\\]$\");","tryCatchPattern":"try { handler.AddChartSeries(parentPath, properties); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"series must be added to a chart parent\"))\n{\n    Console.Error.WriteLine($\"{ex.Message} Correct format: /SheetName/chart[N]\");\n}","preventionTips":["Always use the exact path format /SheetName/chart[N] for chart-series add.","Use 1-based chart indices in square brackets.","Capture and reuse the return path from chart-creation calls to avoid path-construction errors.","Validate the path with a regex before the add call."],"tags":["excel","chart","series","path","validation"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}