{"record":{"id":"498de3cf0e91e181","repo":"iOfficeAI/OfficeCLI","slug":"sheet-not-found-chartsheetname","errorCode":null,"errorMessage":"Sheet not found: {chartSheetName}","messagePattern":"Sheet not found: (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.Chart.cs","lineNumber":27,"sourceCode":"using OfficeCli.Core;\nusing C = DocumentFormat.OpenXml.Drawing.Charts;\nusing Drawing = DocumentFormat.OpenXml.Drawing;\nusing SpreadsheetDrawing = DocumentFormat.OpenXml.Spreadsheet.Drawing;\nusing XDR = DocumentFormat.OpenXml.Drawing.Spreadsheet;\nusing CX = DocumentFormat.OpenXml.Office2016.Drawing.ChartDrawing;\n\nnamespace OfficeCli.Handlers;\n\n// Per-element-type Add helpers for chart paths and the generic-XML default fallback. Mechanically extracted from the Add() god-method.\npublic partial class ExcelHandler\n{\n    private string AddChart(string parentPath, string type, InsertPosition? position, Dictionary<string, string> properties)\n    {\n        var index = position?.Index;\n        var chartSegments = parentPath.TrimStart('/').Split('/', 2);\n        var chartSheetName = chartSegments[0];\n        var chartWorksheet = FindWorksheet(chartSheetName)\n            ?? throw new ArgumentException($\"Sheet not found: {chartSheetName}\");\n\n        // Parse chart data. Use TryGetValue(case-insensitive) so reads\n        // are recorded by TrackingPropertyDictionary in handler-as-truth path.\n        string chartType = \"column\";\n        if (properties.TryGetValue(\"charttype\", out var ctVal) || properties.TryGetValue(\"type\", out ctVal))\n            chartType = ctVal;\n        var chartTitle = properties.GetValueOrDefault(\"title\");\n\n        // Support dataRange: read cell data from worksheet and build series with cell references\n        string[]? categories;\n        List<(string name, double[] values)> seriesData;\n        string? dataRangeStr = null;\n        if (properties.TryGetValue(\"datarange\", out var dr) || properties.TryGetValue(\"range\", out dr))\n            dataRangeStr = dr;\n        ChartRangeGeometry? dataRangeGeometry = null;\n        if (!string.IsNullOrEmpty(dataRangeStr))\n        {\n            (seriesData, categories, dataRangeGeometry) = ParseDataRangeForChart(dataRangeStr, chartSheetName, properties);","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.Chart.cs#L9-L45","documentation":"Thrown by AddChart when the first segment of parentPath (the sheet name) does not resolve to a worksheet via FindWorksheet (case-insensitive). AddChart needs the worksheet both as the data source context (for dataRange/cell-reference qualification) and as the drawing anchor host. Without a resolved worksheet, chart construction cannot proceed.","triggerScenarios":"Calling Add with parentPath '/<sheet>/...' and type=chart (or a chart element type) where <sheet> is not a worksheet in the workbook. The sheet name is the first path segment after trimming the leading '/'.","commonSituations":"Stale sheet name; typo; targeting a chart-sheet (which is not a worksheet part) instead of a data worksheet; workbook from a template with localized sheet names.","solutions":["Use the exact worksheet name that holds the chart's source data as segment[0].","Create the worksheet if it does not exist.","List sheets to confirm names before the call."],"exampleFix":"// before: 'Dashboard' sheet missing\nadd /Dashboard/A1 chart charttype=column title=Sales\n// after\nadd-sheet Dashboard\nadd /Dashboard/A1 chart charttype=column title=Sales","handlingStrategy":"validation","validationCode":"var sheet = parentPath.TrimStart('/').Split('/', 2)[0];\nif (FindWorksheet(sheet) is null)\n    throw new ArgumentException($\"Sheet '{sheet}' not found.\");","typeGuard":"static bool SheetExists(string? sheetName, IEnumerable<string> known)\n    => sheetName is not null && known.Contains(sheetName, StringComparer.OrdinalIgnoreCase);","tryCatchPattern":"try { return Add(path, \"chart\", pos, props); }\ncatch (ArgumentException ex) when (ex.Message.StartsWith(\"Sheet not found\"))\n{ /* correct sheet, retry */ throw; }","preventionTips":["Resolve the target worksheet (data source) before constructing the chart path.","Create the worksheet first if missing.","Use a worksheet name, not a chart-sheet label."],"tags":["excel","chart","sheet-resolution","path-validation"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}