{"record":{"id":"ea4590c924c62e36","repo":"iOfficeAI/OfficeCLI","slug":"cannot-add-a-series-the-chart-has-no-existing-ser","errorCode":null,"errorMessage":"Cannot add a series: the chart has no existing series to derive structure from. Recreate the chart with the desired series instead.","messagePattern":"Cannot add a series: the chart has no existing series to derive structure from\\. Recreate the chart with the desired series instead\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.Chart.cs","lineNumber":426,"sourceCode":"            if (cells != null)\n                properties[\"values\"] = string.Join(\",\", cells.Select(v =>\n                    double.TryParse(v, System.Globalization.CultureInfo.InvariantCulture, out var n) ? n : 0));\n            else\n                properties.Remove(\"values\");\n        }\n        if (properties.TryGetValue(\"categories\", out var catRaw))\n        {\n            properties.Remove(\"categories\"); // ChartHelper.AddSeries doesn't consume it\n            if (ChartHelper.IsRangeReference(catRaw))\n            {\n                categoriesRef = ChartHelper.NormalizeRangeReference(catRaw, sheetName);\n                cachedCats = ResolveRangeToCellValues(catRaw, sheetName);\n            }\n        }\n\n        var newIdx = ChartHelper.AddSeries(chartPart, properties);\n        if (newIdx == 0)\n            throw new ArgumentException(\n                \"Cannot add a series: the chart has no existing series to derive structure from. Recreate the chart with the desired series instead.\");\n        ChartHelper.ApplySeriesRangeRefs(chartPart, newIdx, valuesRef, categoriesRef, cachedCats);\n        return $\"/{sheetName}/chart[{chartIdx}]/series[{newIdx}]\";\n    }\n\n    private string AddDefault(string parentPath, string type, InsertPosition? position, Dictionary<string, string> properties)\n    {\n        var index = position?.Index;\n        // Generic fallback: create typed element via SDK schema validation\n        // Parse parentPath: /<SheetName>/xmlPath...\n        var fbSegments = parentPath.TrimStart('/').Split('/', 2);\n        var fbSheetName = fbSegments[0];\n        var fbWorksheet = FindWorksheet(fbSheetName);\n        if (fbWorksheet == null)\n            throw new ArgumentException($\"Sheet not found: {fbSheetName}\");\n\n        OpenXmlElement fbParent = GetSheet(fbWorksheet);\n        if (fbSegments.Length > 1 && !string.IsNullOrEmpty(fbSegments[1]))","sourceCodeStart":408,"sourceCodeEnd":444,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.Chart.cs#L408-L444","documentation":"Thrown when ChartHelper.AddSeries returns 0, meaning the chart's plot area has no existing series to use as a structural template. AddSeries derives the new series' type, axis, and formatting from the last existing series; an empty chart has nothing to clone from. The fix is to recreate the chart rather than try to bootstrap series into a series-less chart.","triggerScenarios":"Calling add /Sheet/chart[N] --type chart-series on a standard chart that was created without any series (e.g. a chart created from an empty dataRange that was later partially repaired, or a chart whose series were all deleted). ChartHelper.AddSeries cannot find a base series and returns index 0.","commonSituations":"A chart was created with invalid or empty data and survived as a chart object with zero series. A prior operation deleted all series. The chart is structurally present but functionally empty.","solutions":["Delete the empty chart and recreate it with at least one series via data= or dataRange=.","Provide full series data at chart creation time instead of relying on append.","Inspect the chart's plot area to confirm it has zero series before attempting append."],"exampleFix":"// before (chart[1] exists but has 0 series)\nadd /Sheet1/chart[1] --type chart-series --data \"S1:1,2,3\"\n// after (delete and recreate with initial series)\nremove /Sheet1/chart[1]\nadd /Sheet1/chart --type chart --chartType bar --data \"S1:1,2,3\"","handlingStrategy":"try-catch","validationCode":"// Inspect the chart's plot area for existing series before appending\nvar plotArea = chartPart.ChartSpace.PlotArea;\nvar hasSeries = plotArea.Descendants<DocumentFormat.OpenXml.Drawing.Charts.LineSeries>().Any()\n    || plotArea.Descendants<DocumentFormat.OpenXml.Drawing.Charts.BarSeries>().Any()\n    || plotArea.Descendants<DocumentFormat.OpenXml.Drawing.Charts.PieSeries>().Any();\nif (!hasSeries)\n    throw new InvalidOperationException(\n        \"Chart has no existing series. Recreate the chart with initial series data.\");","typeGuard":null,"tryCatchPattern":"try { handler.AddChartSeries(parentPath, properties); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"no existing series to derive structure\"))\n{\n    Console.Error.WriteLine($\"{ex.Message} Delete and recreate the chart.\");\n}","preventionTips":["Always create charts with at least one series so AddSeries has a template to clone.","Avoid deleting all series from a chart; recreate instead.","Inspect the plot area for existing series before attempting an append."],"tags":["excel","chart","series","empty-chart","validation"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}