PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception
No more than 2 rules are allowed in a Custom Filter
Error message
No more than 2 rules are allowed in a Custom Filter
What it means
Switching a column's filter type to 'customFilters' is refused when the column already holds more than two rules, because Excel custom filters support at most two criteria joined by AND/OR. The check happens inside setFilterType() after the type itself has been validated.
Source
Thrown at src/PhpSpreadsheet/Worksheet/AutoFilter/Column.php:172
*/
public function getFilterType(): string
{
return $this->filterType;
}
/**
* Set AutoFilter Type.
*
* @return $this
*/
public function setFilterType(string $filterType): static
{
$this->setEvaluatedFalse();
if (!in_array($filterType, self::$filterTypes)) {
throw new PhpSpreadsheetException('Invalid filter type for column AutoFilter.');
}
if ($filterType === self::AUTOFILTER_FILTERTYPE_CUSTOMFILTER && count($this->ruleset) > 2) {
throw new PhpSpreadsheetException('No more than 2 rules are allowed in a Custom Filter');
}
$this->filterType = $filterType;
return $this;
}
/**
* Get AutoFilter Multiple Rules And/Or Join.
*/
public function getJoin(): string
{
return $this->join;
}
/**
* Set AutoFilter Multiple Rules And/Or.
*View on GitHub (pinned to 65b080eef4)
Solutions
- Reduce the ruleset to at most 2 rules (deleteRule() or clear rules) before switching the type
- Set the filter type first, then add rules — createRule() enforces the same cap going forward
- Use filterType 'filters' when you need to match many values
Example fix
// before
$column->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER); // ruleset already has 3 rules
// after
while (count($column->getRules()) > 2) {
$column->deleteRule(0);
}
$column->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER); Defensive patterns
Strategy: validation
Validate before calling
if (count($column->getRules()) > 2) {
throw new InvalidArgumentException('Reduce rules to <= 2 before switching to customFilters');
}
$column->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER); Try / catch
try {
$column->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER);
} catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
while (count($column->getRules()) > 2) {
$column->deleteRule(0);
}
$column->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER);
} Prevention
- Set the custom filter type before adding rules
- Model the Excel two-criteria limit in your own filter builder UI/API
When it happens
Trigger: Adding 3+ rules via createRule()/addRule() while the column type is not yet customFilters, then calling setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER).
Common situations: Programmatically building several filter rules first and deciding the type afterwards; migrating a many-value filter to a custom filter in one step.
Related errors
- {$range} is an invalid range for AutoFilter
- No autofilter range is defined.
- Column is outside of current autofilter range.
- invalid dynamic rule type $dynamicRuleType
- Invalid filter type for column AutoFilter.
AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17).
Data as JSON: /api/errors/eb08f48b780bf501.
Report an issue: GitHub.