PHPOffice/PhpSpreadsheet · error · SpreadsheetException

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

Error message

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

What it means

getHyperlink() needs a bound parent to resolve the worksheet and coordinate, then returns the existing Hyperlink or a newly created default one via Worksheet::getHyperlink(). On a detached Cell the lookup is impossible and the exception is thrown before any hyperlink object is created.

Source

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

     */
    public function hasHyperlink(): bool
    {
        if (!isset($this->parent)) {
            throw new SpreadsheetException('Cannot check for hyperlink when cell is not bound to a worksheet');
        }

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

    /**
     * Get Hyperlink.
     *
     * @throws SpreadsheetException
     */
    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()

View on GitHub (pinned to 65b080eef4)

Solutions

  1. Use the worksheet API with a coordinate string: $sheet->getHyperlink('A1')
  2. Guard with $cell->getParent() !== null before calling
  3. Carry coordinates, not Cell objects, across stage boundaries

Example fix

// before
$url = $cell->getHyperlink()->getUrl(); // throws when detached

// after
$url = $sheet->getHyperlink('A1')->getUrl();
Defensive patterns

Strategy: validation

Validate before calling

if ($cell->getParent() !== null) {
    $url = $cell->getHyperlink()->getUrl();
} else {
    $url = $sheet->getHyperlink($coord)->getUrl();
}

Type guard

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

Try / catch

null

Prevention

When it happens

Trigger: Calling getHyperlink() on a Cell kept after its worksheet was removed or the spreadsheet discarded; deferred consumers of Cell references (queues, iterators, closures) running post-teardown.

Common situations: Export enrichers that read link URLs from earlier-captured cells; multi-stage CLI pipelines that free the spreadsheet between stages.

Related errors


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