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
- Write through the worksheet by coordinate: $sheet->setHyperlink('A1', $hyperlink)
- Flush hyperlink writes before removing sheets or unsetting the spreadsheet
- 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
- Write hyperlinks through Worksheet::setHyperlink($coord, $hyperlink) using coordinate strings
- Complete all link writes before sheet removal or spreadsheet teardown
- Wrap deferred-write flushes with a liveness check on the target sheet
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
- Cannot check for hyperlink when cell is not bound to a works
- Cannot get hyperlink for cell that is not bound to a workshe
- 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
AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17).
Data as JSON: /api/errors/819364e7ad4f6981.
Report an issue: GitHub.