PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception
No autofilter range is defined.
Error message
No autofilter range is defined.
What it means
AutoFilter column operations that go through testColumnInRange() (getColumnOffset(), rule setup) need an existing filter range to compute offsets. If setRange() was never called — or was cleared with an empty string — this exception signals the AutoFilter has no range yet.
Source
Thrown at src/PhpSpreadsheet/Worksheet/AutoFilter.php:179
*
* @return AutoFilter\Column[]
*/
public function getColumns(): array
{
return $this->columns;
}
/**
* 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
*/View on GitHub (pinned to 65b080eef4)
Solutions
- Call setRange('A1:F20') (or setRange($sheet->calculateWorksheetDimension())) before touching columns
- Guard with $autoFilter->getRange() !== '' and lazily initialize the range on demand
Example fix
// before
$column = $sheet->getAutoFilter()->getColumn('C');
// after
$autoFilter = $sheet->getAutoFilter();
if ($autoFilter->getRange() === '') {
$autoFilter->setRange($sheet->calculateWorksheetDimension());
}
$column = $autoFilter->getColumn('C'); Defensive patterns
Strategy: validation
Validate before calling
if ($autoFilter->getRange() === '') {
$autoFilter->setRange($sheet->calculateWorksheetDimension());
}
$offset = $autoFilter->getColumnOffset('B'); Try / catch
try {
$autoFilter->getColumnOffset($col);
} catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
// no range yet: initialize it and retry
$autoFilter->setRange($sheet->calculateWorksheetDimension());
$autoFilter->getColumnOffset($col);
} Prevention
- Treat setRange() as the first autofilter call in any setup routine
- Wrap autofilter setup in one helper that guarantees a range exists
When it happens
Trigger: $autoFilter->getColumnOffset('B') or creating column rules before any setRange('A1:F20'); also any column call after setRange('') which resets the range and discards column rules.
Common situations: Copy-pasted filtering code where the setRange() line was lost; conditional code paths that skip range setup for empty sheets; clearing the range then reusing column objects.
Related errors
- {$range} is an invalid range for AutoFilter
- Invalid Worksheet for specified Range
- Column is outside of current autofilter range.
- Cannot update when cell is not bound to a worksheet
- Cannot get column when cell is not bound to a worksheet
AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17).
Data as JSON: /api/errors/682ecb3a8edd9f6b.
Report an issue: GitHub.