{"record":{"id":"682ecb3a8edd9f6b","repo":"PHPOffice/PhpSpreadsheet","slug":"no-autofilter-range-is-defined","errorCode":null,"errorMessage":"No autofilter range is defined.","messagePattern":"No autofilter range is defined\\.","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Worksheet/AutoFilter.php","lineNumber":179,"sourceCode":"     *\n     * @return AutoFilter\\Column[]\n     */\n    public function getColumns(): array\n    {\n        return $this->columns;\n    }\n\n    /**\n     * Validate that the specified column is in the AutoFilter range.\n     *\n     * @param string $column Column name (e.g. A)\n     *\n     * @return int The column offset within the autofilter range\n     */\n    public function testColumnInRange(string $column): int\n    {\n        if (empty($this->range)) {\n            throw new Exception('No autofilter range is defined.');\n        }\n\n        $columnIndex = Coordinate::columnIndexFromString($column);\n        [$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($this->range);\n        if (($rangeStart[0] > $columnIndex) || ($rangeEnd[0] < $columnIndex)) {\n            throw new Exception('Column is outside of current autofilter range.');\n        }\n\n        return $columnIndex - $rangeStart[0];\n    }\n\n    /**\n     * Get a specified AutoFilter Column Offset within the defined AutoFilter range.\n     *\n     * @param string $column Column name (e.g. A)\n     *\n     * @return int The offset of the specified column within the autofilter range\n     */","sourceCodeStart":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Worksheet/AutoFilter.php#L161-L197","documentation":"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.","triggerScenarios":"$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.","commonSituations":"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.","solutions":["Call setRange('A1:F20') (or setRange($sheet->calculateWorksheetDimension())) before touching columns","Guard with $autoFilter->getRange() !== '' and lazily initialize the range on demand"],"exampleFix":"// before\n$column = $sheet->getAutoFilter()->getColumn('C');\n\n// after\n$autoFilter = $sheet->getAutoFilter();\nif ($autoFilter->getRange() === '') {\n    $autoFilter->setRange($sheet->calculateWorksheetDimension());\n}\n$column = $autoFilter->getColumn('C');","handlingStrategy":"validation","validationCode":"if ($autoFilter->getRange() === '') {\n    $autoFilter->setRange($sheet->calculateWorksheetDimension());\n}\n$offset = $autoFilter->getColumnOffset('B');","typeGuard":null,"tryCatchPattern":"try {\n    $autoFilter->getColumnOffset($col);\n} catch (\\PhpOffice\\PhpSpreadsheet\\Exception $e) {\n    // no range yet: initialize it and retry\n    $autoFilter->setRange($sheet->calculateWorksheetDimension());\n    $autoFilter->getColumnOffset($col);\n}","preventionTips":["Treat setRange() as the first autofilter call in any setup routine","Wrap autofilter setup in one helper that guarantees a range exists"],"tags":["autofilter","initialization","range","worksheet","phpspreadsheet"],"backgroundTag":"uninitialized-state","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}