iOfficeAI/OfficeCLI · error · ArgumentException

Unknown element type '{type}' for {parentPath}. Valid types:

Error message

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.

What it means

Thrown by AddDefault when GenericXmlQuery.TryCreateTypedElement returns null, meaning the requested element type string is not recognized by the schema-driven factory. The error lists all valid type names so the user can self-correct without consulting separate docs.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Add.Chart.cs:453

        // 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:
    //   row 1     = headers (A1 empty, B1+ = series names)
    //   rows 2..  = data (col A = categories, col B+ = series values)
    private void WriteChartExInlineDataToSheet(
        WorksheetPart worksheetPart,
        string[]? categories,

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Use one of the listed valid types: sheet, row, cell, shape, chart, ole, autofilter, databar, colorscale, iconset, formulacf, comment, namedrange, table, picture, validation, pivottable.
  2. For OLE objects use 'ole' (not 'object' or 'embed').
  3. For pictures use 'picture' (not 'image').
  4. Run 'officecli xlsx add' with no arguments to see the usage/help.

Example fix

// before
add /Sheet1 --type image --src logo.png
// after
add /Sheet1 --type picture --src logo.png
Defensive patterns

Strategy: validation

Validate before calling

// Validate the element type against the known set before calling AddDefault
var validTypes = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
    "sheet", "row", "cell", "shape", "chart", "ole",
    "autofilter", "databar", "colorscale", "iconset", "formulacf",
    "comment", "namedrange", "table", "picture", "validation", "pivottable"
};
if (!validTypes.Contains(type))
    throw new InvalidOperationException(
        $"Unknown element type '{type}'. Valid: {string.Join(", ", validTypes.OrderBy(t => t))}.");

Type guard

static readonly HashSet<string> ValidExcelAddTypes = new(StringComparer.OrdinalIgnoreCase)
{
    "sheet", "row", "cell", "shape", "chart", "ole",
    "autofilter", "databar", "colorscale", "iconset", "formulacf",
    "comment", "namedrange", "table", "picture", "validation", "pivottable"
};
static bool IsValidExcelAddType(string type) => ValidExcelAddTypes.Contains(type);

Try / catch

try { handler.AddDefault(parentPath, type, position, properties); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Unknown element type"))
{
    Console.Error.WriteLine(ex.Message);
}

Prevention

When it happens

Trigger: Calling add with --type set to a value not in the supported set: sheet, row, cell, shape, chart, ole, autofilter, databar, colorscale, iconset, formulacf, comment, namedrange, table, picture, validation, pivottable. Typos like 'cells', 'chart-series' (which is handled separately), or 'object' instead of 'ole' trigger this.

Common situations: User guesses a type name or uses a synonym (e.g. 'image' for 'picture', 'filter' for 'autofilter'). User uses a type valid in a different handler (Word/PPT) that Excel does not support. Copy-paste from outdated docs.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/2b011466c51c1c24. Report an issue: GitHub.