{"record":{"id":"1bca05fc060f532d","repo":"PHPOffice/PhpSpreadsheet","slug":"column-column-is-out-of-range-this-startcolum","errorCode":null,"errorMessage":"Column $column is out of range ({$this->startColumnIndex} - {$this->endColumnIndex})","messagePattern":"Column \\$column is out of range \\((.+?) - (.+?)\\)","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Worksheet/ColumnIterator.php","lineNumber":109,"sourceCode":"    {\n        $endColumn = $endColumn ?: $this->worksheet->getHighestColumn();\n        $this->endColumnIndex = Coordinate::columnIndexFromString($endColumn);\n\n        return $this;\n    }\n\n    /**\n     * Set the column pointer to the selected column.\n     *\n     * @param string $column The column address to set the current pointer at\n     *\n     * @return $this\n     */\n    public function seek(string $column = 'A'): static\n    {\n        $column = Coordinate::columnIndexFromString($column);\n        if (($column < $this->startColumnIndex) || ($column > $this->endColumnIndex)) {\n            throw new PhpSpreadsheetException(\n                \"Column $column is out of range ({$this->startColumnIndex} - {$this->endColumnIndex})\"\n            );\n        }\n        $this->currentColumnIndex = $column;\n\n        return $this;\n    }\n\n    /**\n     * Rewind the iterator to the starting column.\n     */\n    public function rewind(): void\n    {\n        $this->currentColumnIndex = $this->startColumnIndex;\n    }\n\n    /**\n     * Return the current column in this worksheet.","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Worksheet/ColumnIterator.php#L91-L127","documentation":"ColumnIterator walks the columns of a worksheet between a start and end column (defaults 'A' to the sheet's highest column, narrowable via the constructor or resetStart/resetEnd). seek() repositions the pointer to a given column address and throws when that column's numeric index (A=1, B=2, ...) is outside the iterator's bounds. The message prints numeric indexes, so 'Column 7 is out of range (1 - 5)' means column G was requested on an iterator that ends at column E.","triggerScenarios":"(new ColumnIterator($sheet, 'B', 'F'))->seek('G') — beyond the narrowed end column; seek('A') when the iterator starts at 'C'; seeking a column found by a header lookup on the full sheet while iterating only a sub-range.","commonSituations":"Iterating a column subset (e.g. B:F) but reusing a coordinate computed for the whole sheet; off-by-one column math combined with Coordinate::stringFromColumnIndex; running seek() on an iterator built for a previous, wider dataset (e.g. after loading a smaller file).","solutions":["Seek only columns within the range, or widen the range when constructing the iterator: new ColumnIterator($sheet, 'B', 'H') or $iterator->resetEnd('H').","Compare Coordinate::columnIndexFromString($column) against the same start/end values you gave the iterator before calling seek().","If the target may legitimately be absent, wrap seek() in a try/catch for PhpSpreadsheetException and fall back to rewind() or skip."],"exampleFix":"// before\n$iterator = new ColumnIterator($sheet, 'B', 'F');\n$iterator->seek('H'); // Column 8 is out of range (2 - 6)\n\n// after\n$idx = Coordinate::columnIndexFromString('H');\nif ($idx >= 2 && $idx <= 6) {\n    $iterator->seek('H');\n}","handlingStrategy":"validation","validationCode":"$start = Coordinate::columnIndexFromString('B');\n$end = Coordinate::columnIndexFromString('F');\n$idx = Coordinate::columnIndexFromString($wantedColumn);\nif ($idx < $start) {\n    $wantedColumn = 'B';\n} elseif ($idx > $end) {\n    $wantedColumn = 'F'; // clamp to the iterator's end column\n}\n(new ColumnIterator($sheet, 'B', 'F'))->seek($wantedColumn);","typeGuard":"function columnWithinRange(string $column, string $start, string $end): bool\n{\n    $i = Coordinate::columnIndexFromString($column);\n    return $i >= Coordinate::columnIndexFromString($start)\n        && $i <= Coordinate::columnIndexFromString($end);\n}","tryCatchPattern":"try {\n    $iterator->seek($column);\n} catch (PhpSpreadsheetException $e) {\n    // column outside iterator range: widen the range or skip gracefully\n    $iterator->rewind();\n}","preventionTips":["Construct the iterator and compute seek targets from the same start/end bounds","Convert addresses to numeric indexes with Coordinate::columnIndexFromString before comparing","Remember the exception message reports numeric indexes, so 'Column 7' means column G"],"tags":["php","phpspreadsheet","iterator","column","out-of-range"],"backgroundTag":"index-out-of-bounds","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}