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

  1. Use $cell->getWorksheetOrNull() and handle the null case explicitly
  2. Guard the chain yourself: $cell->getParent()?->getParent() before calling getWorksheet()
  3. Refetch cells from a live spreadsheet via coordinates instead of keeping references
  4. 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

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


AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17). Data as JSON: /api/errors/425653f2bc11e908. Report an issue: GitHub.