iOfficeAI/OfficeCLI · error · ArgumentException

criteria{colId}.{op} requires a numeric value, got: '{rawVal

Error message

criteria{colId}.{op} requires a numeric value, got: '{rawVal}'

What it means

Thrown by AddAutoFilter when a Top-N / Bottom-N filter operator (top, topPercent, bottom, bottomPercent) receives a value that is not parseable as a double using InvariantCulture. Excel's <top10> element requires a numeric Val attribute, so a non-numeric string cannot be serialized correctly. The handler validates up front rather than emitting a bad part.

Source

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

                            customEntries.Add((FilterOperatorValues.LessThanOrEqual, hi));
                            customFilterAnd = true;
                        }
                        else
                        {
                            // notBetween = lt lo OR gt hi (Excel default OR)
                            customEntries.Add((FilterOperatorValues.LessThan, lo));
                            customEntries.Add((FilterOperatorValues.GreaterThan, hi));
                        }
                        break;
                    }
                    case "top":
                    case "toppercent":
                    case "bottom":
                    case "bottompercent":
                    {
                        if (!double.TryParse(rawVal, System.Globalization.NumberStyles.Any,
                                System.Globalization.CultureInfo.InvariantCulture, out var topN))
                            throw new ArgumentException(
                                $"criteria{colId}.{op} requires a numeric value, got: '{rawVal}'");
                        filterColumn.Top10 = new Top10
                        {
                            Top = op == "top" || op == "toppercent",
                            Percent = op == "toppercent" || op == "bottompercent",
                            Val = topN
                        };
                        handledDedicated = true;
                        break;
                    }
                    case "blanks":
                        if (IsTruthy(rawVal))
                        {
                            filterColumn.Filters = new Filters { Blank = true };
                            handledDedicated = true;
                        }
                        break;
                    case "nonblanks":

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Pass a plain numeric value, e.g. criteria2.top=10 or criteria2.toppercent=25.
  2. For percentages use a dot as the decimal separator: criteria2.toppercent=12.5.
  3. Ensure the value is non-empty; an empty string fails the double.TryParse.

Example fix

// before
--prop criteria2.top=ten
// after
--prop criteria2.top=10
Defensive patterns

Strategy: validation

Validate before calling

// Validate top/bottom filter values parse as invariant-culture doubles before Add.
static bool TryValidateTopN(string op, string rawVal, out string error)
{
    error = null;
    if (op is not ("top" or "toppercent" or "bottom" or "bottompercent")) return true;
    if (!double.TryParse(rawVal, System.Globalization.NumberStyles.Any,
            System.Globalization.CultureInfo.InvariantCulture, out _))
    {
        error = $"criteria.{op} requires a numeric value, got: '{rawVal}'";
        return false;
    }
    return true;
}

Try / catch

try { handler.Add(path, "autofilter", null, props); }
catch (ArgumentException ex) when (ex.Message.Contains("requires a numeric value"))
{ /* surface: top/bottom filters need an invariant-culture number */ }

Prevention

When it happens

Trigger: Calling Add with a property like criteria2.top=ten, criteria2.toppercent=, criteria2.bottom=abc, or criteria2.bottompercent=1,5 (comma-decimal in an invariant-culture parse).

Common situations: Typing the count as a word, leaving the value empty, or passing a locale-formatted number where the comma is the decimal separator (InvariantCulture expects a dot).

Related errors


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