iOfficeAI/OfficeCLI · error · ArgumentException

Invalid element type '{type}' for xlsx files (belongs to {so

Error message

Invalid element type '{type}' for xlsx files (belongs to {sourceFormat}). Valid values: sheet, row, cell, col, namedrange, comment, validation, autofilter, cf, databar, colorscale, iconset, formulacf, cellis, ole, picture, shape, slicer, sparkline, table, chart, pivottable.

What it means

Thrown by ExcelHandler.Add's front-door format guard when the requested element type belongs to a different Office format (pptx or docx) rather than xlsx. Without this guard, e.g. 'add --type slide' would fall into AddDefault and produce a misleading 'Sheet not found:' with an empty name. The guard maps the wrong-format type to its source format and lists every valid xlsx type so the caller knows where to look.

Source

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

        // front. Without this, "add /xlsx --type slide" fell into AddDefault,
        // tried to resolve the parent sheet from an empty path segment, and
        // produced a misleading "Sheet not found: " error with an empty name.
        // Naming the wrong-format type explicitly tells the caller where to
        // look (e.g. use the .pptx variant) instead of sending them on a
        // sheet-permission hunt.
        var typeLower = type.ToLowerInvariant();
        if (typeLower is "slide" or "slidemaster" or "slidelayout" or "notes"
            or "paragraph" or "p" or "field"
            or "section" or "header" or "footer")
        {
            var sourceFormat = typeLower switch
            {
                "slide" or "slidemaster" or "slidelayout" or "notes" => "pptx",
                "paragraph" or "p" or "field" => "docx/pptx",
                "section" or "header" or "footer" => "docx",
                _ => "another format"
            };
            throw new ArgumentException(
                $"Invalid element type '{type}' for xlsx files (belongs to {sourceFormat}). " +
                "Valid values: sheet, row, cell, col, namedrange, comment, validation, autofilter, " +
                "cf, databar, colorscale, iconset, formulacf, cellis, ole, picture, shape, slicer, " +
                "sparkline, table, chart, pivottable.");
        }

        switch (type.ToLowerInvariant())
        {
            case "sheet":
                return AddSheet(parentPath, type, position, properties);

            case "row":
                return AddRow(parentPath, type, position, properties);

            case "cell":
                return AddCell(parentPath, type, position, properties);

            case "namedrange" or "definedname" or "name":

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Use a valid xlsx element type: sheet, row, cell, col, namedrange, comment, validation, autofilter, cf, databar, colorscale, iconset, formulacf, cellis, ole, picture, shape, slicer, sparkline, table, chart, pivottable.
  2. If you intended a pptx/docx element, target the corresponding pptx/docx handler instead of the xlsx one.
  3. Double-check the file extension and handler selection in the calling code.

Example fix

// before
add /xlsx --type slide
// after (for xlsx)
add /xlsx --type sheet
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

static bool IsValidXlsxType(string type) => ValidXlsxTypes.Contains(type);

Try / catch

try { handler.Add(parentPath, type, null, props); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Invalid element type"))
{ /* route to the correct handler (pptx/docx) or use a valid xlsx type */ }

Prevention

When it happens

Trigger: Calling Add on an xlsx handler with type=slide, slidemaster, slidelayout, notes (pptx); paragraph, p, field (docx/pptx); or section, header, footer (docx).

Common situations: Wrong handler invoked for the file type (e.g. opened a .pptx operation on an .xlsx), or a script that reuses element types across formats without branching.

Related errors


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