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

  1. Reduce the ruleset to at most 2 rules (deleteRule() or clear rules) before switching the type
  2. Set the filter type first, then add rules — createRule() enforces the same cap going forward
  3. 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

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


AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17). Data as JSON: /api/errors/eb08f48b780bf501. Report an issue: GitHub.