PHPOffice/PhpSpreadsheet · error · SpreadsheetException
Cannot get row when cell is not bound to a worksheet
Error message
Cannot get row when cell is not bound to a worksheet
What it means
A Cell object is a thin wrapper whose $parent property links it to the Cells collection of a worksheet; getRow() simply delegates to $parent->getCurrentRow(). When the Cell is no longer attached to a collection (worksheet removed, collection emptied or detached, or the Cell object outlived it), $parent is null and this exception is thrown. The row number only exists as metadata owned by the live collection, not by the Cell itself.
Source
Thrown at src/PhpSpreadsheet/Cell/Cell.php:149
{
$parent = $this->parent;
if ($parent === null) {
throw new SpreadsheetException('Cannot get column when cell is not bound to a worksheet');
}
return $parent->getCurrentColumn();
}
/**
* Get cell coordinate row.
*
* @throws SpreadsheetException
*/
public function getRow(): int
{
$parent = $this->parent;
if ($parent === null) {
throw new SpreadsheetException('Cannot get row when cell is not bound to a worksheet');
}
return $parent->getCurrentRow();
}
/**
* Get cell coordinate.
*
* @throws SpreadsheetException
*/
public function getCoordinate(): string
{
$parent = $this->parent;
if ($parent !== null) {
$coordinate = $parent->getCurrentCoordinate();
} else {
$coordinate = null;
}View on GitHub (pinned to 65b080eef4)
Solutions
- Capture the row number (or the full coordinate string) as a plain int/string immediately after getCell(), instead of holding the Cell object
- Guard the call with $cell->getParent() !== null (or $cell->getWorksheetOrNull() !== null) before getRow()
- Re-fetch the cell from a live worksheet via $sheet->getCell($coordinate) when you need it later
- Derive the row from a coordinate string you already hold using Coordinate::indexesFromString($coord)
Example fix
// before
$cell = $sheet->getCell('B4');
$spreadsheet->removeSheetByIndex($sheetIndex);
$row = $cell->getRow(); // throws: cell no longer bound to a worksheet
// after
$cell = $sheet->getCell('B4');
$row = $cell->getRow(); // capture while the worksheet is alive
$coord = 'B4';
$spreadsheet->removeSheetByIndex($sheetIndex); Defensive patterns
Strategy: validation
Validate before calling
if ($cell->getParent() === null) {
// cell is detached; recover from a live worksheet
$cell = $spreadsheet->getSheetByName($sheetName)->getCell($coord);
}
$row = $cell->getRow(); Type guard
function cellIsBoundToWorksheet(Cell $cell): bool
{
return $cell->getParent() !== null && $cell->getParent()->getParent() !== null;
} Try / catch
try {
$row = $cell->getRow();
} catch (SpreadsheetException $e) {
$cell = $sheet->getCell($coord); // re-bind via a live worksheet
$row = $cell->getRow();
} Prevention
- Store coordinate strings or [row, col] ints across lifecycle boundaries, never Cell objects
- Call getRow()/getCoordinate() immediately after getCell(), while the sheet is guaranteed alive
- Do not remove sheets or unset the spreadsheet while Cell references are still in use
When it happens
Trigger: Calling $cell->getRow() on a Cell reference kept after $spreadsheet->removeSheetByIndex(), after unset($spreadsheet), after the cell collection was garbage-collected or emptied, or on a Cell obtained from a cloned/detached Cells collection.
Common situations: Caching Cell objects across import/export stages while the spreadsheet is torn down between stages; storing cells in queues or closures that execute after sheet teardown; copying cells between workbooks and then reusing the original references.
Related errors
- Cannot update when cell is not bound to a worksheet
- Cannot get column when cell is not bound to a worksheet
- Cannot check for data validation when cell is not bound to a
- Cannot get data validation for cell that is not bound to a w
- Cannot set data validation for cell that is not bound to a w
AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17).
Data as JSON: /api/errors/a2810aedb691e82e.
Report an issue: GitHub.