iOfficeAI/OfficeCLI · error · ArgumentException
Parent element not found: {parentPath}
Error message
Parent element not found: {parentPath} What it means
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.
Source
Thrown at src/officecli/Handlers/Excel/ExcelHandler.Add.Chart.cs:448
}
private string AddDefault(string parentPath, string type, InsertPosition? position, Dictionary<string, string> properties)
{
var index = position?.Index;
// Generic fallback: create typed element via SDK schema validation
// Parse parentPath: /<SheetName>/xmlPath...
var fbSegments = parentPath.TrimStart('/').Split('/', 2);
var fbSheetName = fbSegments[0];
var fbWorksheet = FindWorksheet(fbSheetName);
if (fbWorksheet == null)
throw new ArgumentException($"Sheet not found: {fbSheetName}");
OpenXmlElement fbParent = GetSheet(fbWorksheet);
if (fbSegments.Length > 1 && !string.IsNullOrEmpty(fbSegments[1]))
{
var xmlSegments = GenericXmlQuery.ParsePathSegments(fbSegments[1]);
fbParent = GenericXmlQuery.NavigateByPath(fbParent!, xmlSegments)
?? throw new ArgumentException($"Parent element not found: {parentPath}");
}
var created = GenericXmlQuery.TryCreateTypedElement(fbParent!, type, properties, index);
if (created == null)
throw new ArgumentException(
$"Unknown element type '{type}' for {parentPath}. " +
"Valid types: sheet, row, cell, shape, chart, ole (object, embed), autofilter, databar, colorscale, iconset, formulacf, comment, namedrange, table, picture, validation, pivottable. " +
"Use 'officecli xlsx add' for details.");
SaveWorksheet(fbWorksheet);
var siblings = fbParent.ChildElements.Where(e => e.LocalName == created.LocalName).ToList();
var createdIdx = PathIndex.FromArrayIndex(siblings.IndexOf(created));
return $"{parentPath}/{created.LocalName}[{createdIdx}]";
}
// Write inline chartEx categories/values into the host sheet at A1..B(N+1).
// cx:f formulas in BuildExtendedChartSpace assume:View on GitHub (pinned to 1ced45e900)
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.
Example fix
// before (row[99] does not exist) add /Sheet1/sheetData/row[99]/c --type cell --value "x" // after (navigate to an existing row) add /Sheet1/sheetData/row[1]/c --type cell --value "x"
Defensive patterns
Strategy: validation
Validate before calling
// Verify the parent XML path resolves before calling AddDefault
var segments = parentPath.TrimStart('/').Split('/', 2);
var sheetName = segments[0];
var worksheet = handler.FindWorksheet(sheetName);
if (worksheet != null && segments.Length > 1 && !string.IsNullOrEmpty(segments[1]))
{
var xmlSegments = GenericXmlQuery.ParsePathSegments(segments[1]);
var parent = GenericXmlQuery.NavigateByPath(handler.GetSheet(worksheet), xmlSegments);
if (parent == null)
throw new InvalidOperationException($"Parent element not found: {parentPath}");
} Try / catch
try { handler.AddDefault(parentPath, type, position, properties); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Parent element not found:"))
{
Console.Error.WriteLine($"{ex.Message} Inspect the worksheet XML structure.");
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Sheet not found: {fbSheetName}
- Unknown element type '{type}' for {parentPath}. Valid types:
- Property 'sqref' (or 'range'/'ref') is required for validati
- invalid_value
- 'add' command requires 'parent' field. Example: {"command":
AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13).
Data as JSON: /api/errors/b74cd72161fbd86a.
Report an issue: GitHub.