PHPOffice/PhpSpreadsheet · error · SpreadsheetException
Worksheet no longer exists
Error message
Worksheet no longer exists
What it means
getWorksheet() resolves the two-hop chain Cell -> Cells (parent) -> Worksheet; if either hop is null (cell detached from its collection, or collection detached from its worksheet, e.g. after removal/destruction), it throws. Use the sibling getWorksheetOrNull() when you want a null instead of an exception during teardown-sensitive code.
Source
Thrown at src/PhpSpreadsheet/Cell/Cell.php:838
return $this->parent;
}
/**
* Get parent worksheet.
*
* @throws SpreadsheetException
*/
public function getWorksheet(): Worksheet
{
$parent = $this->parent;
if ($parent !== null) {
$worksheet = $parent->getParent();
} else {
$worksheet = null;
}
if ($worksheet === null) {
throw new SpreadsheetException('Worksheet no longer exists');
}
return $worksheet;
}
public function getWorksheetOrNull(): ?Worksheet
{
$parent = $this->parent;
if ($parent !== null) {
$worksheet = $parent->getParent();
} else {
$worksheet = null;
}
return $worksheet;
}
/**View on GitHub (pinned to 65b080eef4)
Solutions
- Use $cell->getWorksheetOrNull() and handle the null case explicitly
- Guard the chain yourself: $cell->getParent()?->getParent() before calling getWorksheet()
- Refetch cells from a live spreadsheet via coordinates instead of keeping references
- Restructure so worksheet-dependent work finishes before teardown
Example fix
// before
$ws = $cell->getWorksheet(); // throws when worksheet is gone
// after
$ws = $cell->getWorksheetOrNull();
if ($ws === null) {
$ws = $spreadsheet->getSheetByName($sheetName); // recover via live spreadsheet
} Defensive patterns
Strategy: validation
Validate before calling
$ws = $cell->getWorksheetOrNull();
if ($ws === null) {
$ws = $spreadsheet->getSheetByName($sheetName); // recover or fail explicitly
if ($ws === null) {
throw new RuntimeException("Worksheet '$sheetName' is gone; cannot process cell $coord");
}
} Type guard
function cellIsBoundToWorksheet(Cell $cell): bool
{
return $cell->getParent() !== null && $cell->getParent()->getParent() !== null;
} Try / catch
null
Prevention
- Prefer getWorksheetOrNull() plus an explicit null branch in teardown-sensitive code
- Finish worksheet-dependent work before removing sheets or discarding the spreadsheet
- Keep (sheetName, coordinate) as the canonical cell identity in long pipelines
When it happens
Trigger: Calling getWorksheet() on a Cell retained after unset($spreadsheet) or removeSheetByIndex(); accessing cells during worksheet destruction; debugging variables after the workbook went out of scope.
Common situations: Long pipelines and queue workers holding Cell references; code cloning sheets/cells and reusing originals; exception reporters inspecting cells captured in earlier frames.
Related errors
- Cannot update when cell is not bound to a worksheet
- Cannot get column when cell is not bound to a worksheet
- Cannot get row 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
AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17).
Data as JSON: /api/errors/425653f2bc11e908.
Report an issue: GitHub.