iOfficeAI/OfficeCLI · error · ArgumentException

Unknown operator: {dvOp}

Error message

Unknown operator: {dvOp}

What it means

Thrown by AddValidation when the optional "operator" property is present but its lower-cased value is not one of between, notbetween, equal, notequal, greaterthan, lessthan, greaterthanorequal, lessthanorequal. These map to DataValidationOperatorValues; an unrecognized token is rejected rather than silently defaulting.

Source

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

                "textlength" => DataValidationValues.TextLength,
                "custom" => DataValidationValues.Custom,
                _ => throw new ArgumentException($"Unknown validation type: {dvType}. Use: list, whole, decimal, date, time, textLength, custom")
            };
        }

        if (properties.TryGetValue("operator", out var dvOp))
        {
            dv.Operator = dvOp.ToLowerInvariant() switch
            {
                "between" => DataValidationOperatorValues.Between,
                "notbetween" => DataValidationOperatorValues.NotBetween,
                "equal" => DataValidationOperatorValues.Equal,
                "notequal" => DataValidationOperatorValues.NotEqual,
                "greaterthan" => DataValidationOperatorValues.GreaterThan,
                "lessthan" => DataValidationOperatorValues.LessThan,
                "greaterthanorequal" => DataValidationOperatorValues.GreaterThanOrEqual,
                "lessthanorequal" => DataValidationOperatorValues.LessThanOrEqual,
                _ => throw new ArgumentException($"Unknown operator: {dvOp}")
            };
        }

        if (properties.TryGetValue("formula1", out var dvFormula1))
        {
            // R28-A1 — reject empty formula1 for type=list. Excel renders an empty
            // dropdown (or rejects the file outright depending on form), and the
            // user almost certainly meant to provide options like "1,2,3".
            if (dv.Type?.Value == DataValidationValues.List
                && string.IsNullOrWhiteSpace(dvFormula1.Trim('"')))
                throw new ArgumentException(
                    "Property 'formula1' is empty for validation type=list; supply options like formula1=\"1,2,3\" or a range reference.");
            // Excel caps data-validation formulas at 255 chars; longer ones
            // pass schema validation but the file is refused (0x800A03EC).
            if (dvFormula1.Length > 255)
                throw new ArgumentException(
                    $"validation formula1 is {dvFormula1.Length} chars; Excel's limit is 255. Put the list in a range and reference it instead.");
            // Embedded double quotes inside a literal list (not a range ref)

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Use a token from the set, e.g. greaterThanOrEqual, notBetween (case-insensitive, no spaces).
  2. Translate symbols: >= -> greaterThanOrEqual, <= -> lessThanOrEqual, <> -> notEqual.
  3. Validate the operator against the allowed set before calling.

Example fix

// before
handler.Add("/Sheet1", "validation", null,
    new() { ["sqref"] = "A1:A5", ["type"] = "whole", ["operator"] = ">=" });
// after
handler.Add("/Sheet1", "validation", null,
    new() { ["sqref"] = "A1:A5", ["type"] = "whole", ["operator"] = "greaterThanOrEqual" });
Defensive patterns

Strategy: type-guard

Validate before calling

static readonly HashSet<string> DvOps = new(StringComparer.OrdinalIgnoreCase)
    { "between", "notbetween", "equal", "notequal", "greaterthan", "lessthan",
      "greaterthanorequal", "lessthanorequal" };
if (properties.TryGetValue("operator", out var o) && !DvOps.Contains(o))
    throw new ArgumentOutOfRangeException($"bad operator '{o}'");

Type guard

static bool IsValidValidationOperator(string? o) =>
    o is not null && (new[] { "between", "notbetween", "equal", "notequal", "greaterthan",
        "lessthan", "greaterthanorequal", "lessthanorequal" }).Contains(o, StringComparer.OrdinalIgnoreCase);

Prevention

When it happens

Trigger: Call Add type "validation" with properties["operator"] set to an out-of-set value (e.g. ">=", "gte", "not equal", "gt").

Common situations: Passing a symbol (>=) or abbreviation instead of the camelCase token; including a space; typos; mixing conditional-formatting operator vocabulary with data-validation vocabulary.

Related errors


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