PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception

Sheet does not have a parent.

Error message

Sheet does not have a parent.

What it means

Thrown by Worksheet::getParentOrThrow() when the worksheet's $parent (the owning Spreadsheet) is null — i.e. the sheet exists as a detached object. Many APIs (cross-sheet references, defined names, code-name de-duplication) need the workbook context, so they call getParentOrThrow() and fail fast on detached sheets.

Source

Thrown at src/PhpSpreadsheet/Worksheet/Worksheet.php:850

    /**
     * Get parent or null.
     */
    public function getParent(): ?Spreadsheet
    {
        return $this->parent;
    }

    /**
     * Get parent, throw exception if null.
     */
    public function getParentOrThrow(): Spreadsheet
    {
        if ($this->parent !== null) {
            return $this->parent;
        }

        throw new Exception('Sheet does not have a parent.');
    }

    /**
     * Re-bind parent.
     *
     * @return $this
     */
    public function rebindParent(Spreadsheet $parent): static
    {
        if ($this->parent !== null) {
            $definedNames = $this->parent->getDefinedNames();
            foreach ($definedNames as $definedName) {
                $parent->addDefinedName($definedName);
            }

            $this->parent->removeSheetByIndex(
                $this->parent->getIndex($this)
            );

View on GitHub (pinned to 65b080eef4)

Solutions

  1. Create sheets through the workbook: $spreadsheet->createSheet() or $spreadsheet->addSheet(new Worksheet($spreadsheet))
  2. Pass the parent at construction: new Worksheet($spreadsheet)
  3. For an existing detached sheet, rebind: $sheet->rebindParent($spreadsheet) (works even from a null parent; it just skips defined-name migration)

Example fix

// before
$sheet = new Worksheet();
$sheet->getCell('Summary!A1'); // getParentOrThrow() throws

// after
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->createSheet();
$sheet->getCell('Summary!A1');
Defensive patterns

Strategy: validation

Validate before calling

if ($sheet->getParent() === null) {
    $sheet->rebindParent($spreadsheet);
    // or: $spreadsheet->addSheet($sheet);
}
// safe to use workbook-dependent APIs now

Prevention

When it happens

Trigger: $sheet = new Worksheet(); (constructor parent omitted) followed by anything needing the workbook, e.g. $sheet->getCell('Other!A1') or a Table::setWorksheet() chain; sheets instantiated standalone in unit tests; sheet objects used after being detached from their spreadsheet.

Common situations: Instantiating Worksheet directly instead of through the spreadsheet in tests or helper code; copying a sheet into a new Spreadsheet without rebinding; forgetting that new Worksheet(null) leaves the sheet orphaned until addSheet().

Related errors


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