PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception
Invalid operator for column AutoFilter Rule.
Error message
Invalid operator for column AutoFilter Rule.
What it means
Rule::setOperator() accepts the six comparison operators (equal, notEqual, greaterThan, greaterThanOrEqual, lessThan, lessThanOrEqual) plus the top-ten selectors 'byValue' and 'byPercent'; an empty operator defaults to 'equal'. Anything outside those lists throws.
Source
Thrown at src/PhpSpreadsheet/Worksheet/AutoFilter/Column/Rule.php:309
/**
* Set AutoFilter Rule Operator.
*
* @param string $operator see self::AUTOFILTER_COLUMN_RULE_*
*
* @return $this
*/
public function setOperator(string $operator): static
{
$this->setEvaluatedFalse();
if (empty($operator)) {
$operator = self::AUTOFILTER_COLUMN_RULE_EQUAL;
}
if (
(!in_array($operator, self::OPERATORS))
&& (!in_array($operator, self::TOP_TEN_VALUE))
) {
throw new PhpSpreadsheetException('Invalid operator for column AutoFilter Rule.');
}
$this->operator = $operator;
return $this;
}
/**
* Get AutoFilter Rule Grouping.
*/
public function getGrouping(): string
{
return $this->grouping;
}
/**
* Set AutoFilter Rule Grouping.
*
* @return $thisView on GitHub (pinned to 65b080eef4)
Solutions
- Use Rule::AUTOFILTER_COLUMN_RULE_* constants (e.g. Rule::AUTOFILTER_COLUMN_RULE_GREATERTHANOREQUAL), and 'byValue'/'byPercent' for top-ten rules
- Map external operator symbols ('>' => 'greaterThan') explicitly before calling setOperator()
- Omit the operator entirely when you want equality
Example fix
// before
$rule->setOperator('>=' );
// after
$rule->setOperator(\PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_GREATERTHANOREQUAL); Defensive patterns
Strategy: validation
Validate before calling
use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule;
$map = [
'=' => Rule::AUTOFILTER_COLUMN_RULE_EQUAL,
'<>' => Rule::AUTOFILTER_COLUMN_RULE_NOTEQUAL,
'>' => Rule::AUTOFILTER_COLUMN_RULE_GREATERTHAN,
'>=' => Rule::AUTOFILTER_COLUMN_RULE_GREATERTHANOREQUAL,
'<' => Rule::AUTOFILTER_COLUMN_RULE_LESSTHAN,
'<=' => Rule::AUTOFILTER_COLUMN_RULE_LESSTHANOREQUAL,
];
$operator = $map[$operator] ?? Rule::AUTOFILTER_COLUMN_RULE_EQUAL;
$rule->setOperator($operator); Type guard
function isValidRuleOperator(string $operator): bool
{
$valid = ['equal', 'notEqual', 'greaterThan', 'greaterThanOrEqual', 'lessThan', 'lessThanOrEqual', 'byValue', 'byPercent'];
return in_array($operator, $valid, true);
} Try / catch
try {
$rule->setOperator($operator);
} catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
$rule->setOperator(Rule::AUTOFILTER_COLUMN_RULE_EQUAL);
} Prevention
- Translate symbols/UI labels to AUTOFILTER_COLUMN_RULE_* constants yourself
- For top-ten rules use 'byValue'/'byPercent', not percentages or numbers
When it happens
Trigger: ->setOperator('=='), ->setOperator('contains'), ->setOperator('>') or SQL-style symbols — none are in the accepted vocabulary.
Common situations: Translating SQL/Excel UI operator names or symbols directly to the API; unvalidated operator strings from configuration files.
Related errors
- invalid dynamic rule type $dynamicRuleType
- Invalid filter type for column AutoFilter.
- Invalid rule connection for column AutoFilter.
- Invalid rule type for column AutoFilter Rule.
- Invalid grouping for column AutoFilter Rule.
AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17).
Data as JSON: /api/errors/254d9db32e03dd9e.
Report an issue: GitHub.