PHPOffice/PhpSpreadsheet · error · SpreadsheetException

Cannot set hyperlink for cell that is not bound to a workshe

Error message

Cannot set hyperlink for cell that is not bound to a worksheet

What it means

setHyperlink() writes the hyperlink to the cell's coordinate on its worksheet, then calls updateInCollection() - both steps need a live parent. With the cell detached there is no worksheet to store the hyperlink on, so the method throws immediately.

Source

Thrown at src/PhpSpreadsheet/Cell/Cell.php:806

    public function getHyperlink(): Hyperlink
    {
        if (!isset($this->parent)) {
            throw new SpreadsheetException('Cannot get hyperlink for cell that is not bound to a worksheet');
        }

        return $this->getWorksheet()
            ->getHyperlink($this->getCoordinate());
    }

    /**
     * Set Hyperlink.
     *
     * @throws SpreadsheetException
     */
    public function setHyperlink(?Hyperlink $hyperlink = null): self
    {
        if (!isset($this->parent)) {
            throw new SpreadsheetException('Cannot set hyperlink for cell that is not bound to a worksheet');
        }

        $this->getWorksheet()
            ->setHyperlink($this->getCoordinate(), $hyperlink);

        return $this->updateInCollection();
    }

    /**
     * Get cell collection.
     */
    public function getParent(): ?Cells
    {
        return $this->parent;
    }

    /**
     * Get parent worksheet.

View on GitHub (pinned to 65b080eef4)

Solutions

  1. Write through the worksheet by coordinate: $sheet->setHyperlink('A1', $hyperlink)
  2. Flush hyperlink writes before removing sheets or unsetting the spreadsheet
  3. Guard with $cell->getParent() !== null and drop or requeue detached cells

Example fix

// before
$cell->setHyperlink($hyperlink); // throws when detached

// after
$sheet->setHyperlink('A1', $hyperlink);
Defensive patterns

Strategy: validation

Validate before calling

if ($cell->getParent() !== null) {
    $cell->setHyperlink($hyperlink);
} else {
    $sheet->setHyperlink($coord, $hyperlink);
}

Type guard

function cellIsBoundToWorksheet(Cell $cell): bool
{
    return $cell->getParent() !== null && $cell->getParent()->getParent() !== null;
}

Try / catch

null

Prevention

When it happens

Trigger: Calling setHyperlink($link) on a Cell held after worksheet removal/teardown; applying link changes collected earlier once the target sheet no longer exists.

Common situations: Builders that queue hyperlink assignments and flush at the end; processors that delete intermediate sheets before final writes.

Related errors


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