PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception

Column is outside of current autofilter range.

Error message

Column is outside of current autofilter range.

What it means

testColumnInRange() converts the column letter to a numeric index and verifies it lies between the first and last column of the current AutoFilter range. A column left or right of that range cannot hold filter rules, so the request is rejected.

Source

Thrown at src/PhpSpreadsheet/Worksheet/AutoFilter.php:185

    }

    /**
     * Validate that the specified column is in the AutoFilter range.
     *
     * @param string $column Column name (e.g. A)
     *
     * @return int The column offset within the autofilter range
     */
    public function testColumnInRange(string $column): int
    {
        if (empty($this->range)) {
            throw new Exception('No autofilter range is defined.');
        }

        $columnIndex = Coordinate::columnIndexFromString($column);
        [$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($this->range);
        if (($rangeStart[0] > $columnIndex) || ($rangeEnd[0] < $columnIndex)) {
            throw new Exception('Column is outside of current autofilter range.');
        }

        return $columnIndex - $rangeStart[0];
    }

    /**
     * Get a specified AutoFilter Column Offset within the defined AutoFilter range.
     *
     * @param string $column Column name (e.g. A)
     *
     * @return int The offset of the specified column within the autofilter range
     */
    public function getColumnOffset(string $column): int
    {
        return $this->testColumnInRange($column);
    }

    /**

View on GitHub (pinned to 65b080eef4)

Solutions

  1. Use a column inside the current range, or widen the range first via setRange()
  2. Pre-check: compare Coordinate::columnIndexFromString($col) against rangeBoundaries($autoFilter->getRange()) before access
  3. Recompute the filter range from the sheet dimension before applying rules

Example fix

// before
$autoFilter->getColumn('G')->createRule();

// after
[$start, $end] = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::rangeBoundaries($autoFilter->getRange());
$colIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString('G');
if ($colIndex >= $start[0] && $colIndex <= $end[0]) {
    $autoFilter->getColumn('G')->createRule();
}
Defensive patterns

Strategy: validation

Validate before calling

use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
[$start, $end] = Coordinate::rangeBoundaries($autoFilter->getRange());
$idx = Coordinate::columnIndexFromString($columnLetter);
if ($idx < $start[0] || $idx > $end[0]) {
    $autoFilter->setRange('A1:' . $columnLetter . $sheet->getHighestRow()); // widen
}
$autoFilter->getColumn($columnLetter);

Try / catch

try {
    $autoFilter->getColumn($columnLetter)->createRule();
} catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
    // column out of range: widen the filter range, then retry
}

Prevention

When it happens

Trigger: $autoFilter->getColumn('G')->createRule() or getColumnOffset('G') while the range is A1:F20; any column access outside rangeBoundaries() of the current range.

Common situations: Hard-coded column letters that no longer fit after the sheet layout changed; loops from 'A' to a fixed last column on sheets whose filter range is narrower.

Related errors


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