{"record":{"id":"b74cd72161fbd86a","repo":"iOfficeAI/OfficeCLI","slug":"parent-element-not-found-parentpath","errorCode":null,"errorMessage":"Parent element not found: {parentPath}","messagePattern":"Parent element not found: (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.Chart.cs","lineNumber":448,"sourceCode":"    }\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]))\n        {\n            var xmlSegments = GenericXmlQuery.ParsePathSegments(fbSegments[1]);\n            fbParent = GenericXmlQuery.NavigateByPath(fbParent!, xmlSegments)\n                ?? throw new ArgumentException($\"Parent element not found: {parentPath}\");\n        }\n\n        var created = GenericXmlQuery.TryCreateTypedElement(fbParent!, type, properties, index);\n        if (created == null)\n            throw new ArgumentException(\n                $\"Unknown element type '{type}' for {parentPath}. \" +\n                \"Valid types: sheet, row, cell, shape, chart, ole (object, embed), autofilter, databar, colorscale, iconset, formulacf, comment, namedrange, table, picture, validation, pivottable. \" +\n                \"Use 'officecli xlsx add' for details.\");\n\n        SaveWorksheet(fbWorksheet);\n\n        var siblings = fbParent.ChildElements.Where(e => e.LocalName == created.LocalName).ToList();\n        var createdIdx = PathIndex.FromArrayIndex(siblings.IndexOf(created));\n        return $\"{parentPath}/{created.LocalName}[{createdIdx}]\";\n    }\n\n    // Write inline chartEx categories/values into the host sheet at A1..B(N+1).\n    // cx:f formulas in BuildExtendedChartSpace assume:","sourceCodeStart":430,"sourceCodeEnd":466,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.Chart.cs#L430-L466","documentation":"Thrown by AddDefault when the XML path segment after the sheet name (everything after the first '/') fails to navigate to an existing parent element. GenericXmlQuery.NavigateByPath walks the parsed path segments against the worksheet's XML tree; if any intermediate element is missing, it returns null and this error fires.","triggerScenarios":"Calling add /Sheet1/nonexistent/path --type cell where the intermediate XML path does not resolve. For example, referencing a table or row index that does not exist, or a misspelled element name in the XML path.","commonSituations":"User constructs a path from memory or documentation that doesn't match the actual XML structure. A prior delete removed the parent element. Path references a row or table that was never created.","solutions":["Inspect the worksheet's XML structure (e.g. via get /Sheet1) to find the correct parent path.","Create any missing intermediate elements first.","Simplify the path to target a known-existing parent.","Verify element names and indices match the actual document."],"exampleFix":"// before (row[99] does not exist)\nadd /Sheet1/sheetData/row[99]/c --type cell --value \"x\"\n// after (navigate to an existing row)\nadd /Sheet1/sheetData/row[1]/c --type cell --value \"x\"","handlingStrategy":"validation","validationCode":"// Verify the parent XML path resolves before calling AddDefault\nvar segments = parentPath.TrimStart('/').Split('/', 2);\nvar sheetName = segments[0];\nvar worksheet = handler.FindWorksheet(sheetName);\nif (worksheet != null && segments.Length > 1 && !string.IsNullOrEmpty(segments[1]))\n{\n    var xmlSegments = GenericXmlQuery.ParsePathSegments(segments[1]);\n    var parent = GenericXmlQuery.NavigateByPath(handler.GetSheet(worksheet), xmlSegments);\n    if (parent == null)\n        throw new InvalidOperationException($\"Parent element not found: {parentPath}\");\n}","typeGuard":null,"tryCatchPattern":"try { handler.AddDefault(parentPath, type, position, properties); }\ncatch (ArgumentException ex) when (ex.Message.StartsWith(\"Parent element not found:\"))\n{\n    Console.Error.WriteLine($\"{ex.Message} Inspect the worksheet XML structure.\");\n}","preventionTips":["Inspect the worksheet XML structure (get /SheetName) before constructing deep paths.","Create any missing intermediate elements first.","Use short paths that target known-existing parents.","Validate the full XML path resolves before the add call."],"tags":["excel","add","xml-path","navigation","validation"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}