PHPOffice/PhpSpreadsheet · error · SpreadsheetException
Cannot check for hyperlink when cell is not bound to a works
Error message
Cannot check for hyperlink when cell is not bound to a worksheet
What it means
hasHyperlink() checks $this->parent first because hyperlink existence is stored per coordinate on the worksheet (Worksheet::hyperlinkExists()). A Cell no longer bound to a worksheet cannot be looked up, so the method throws instead of returning false - important distinction: this is a lifecycle error, not 'no hyperlink here'.
Source
Thrown at src/PhpSpreadsheet/Cell/Cell.php:777
/**
* Does this cell contain valid value?
*/
public function hasValidValue(): bool
{
$validator = new DataValidator();
return $validator->isValid($this);
}
/**
* Does this cell contain a Hyperlink?
*
* @throws SpreadsheetException
*/
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());View on GitHub (pinned to 65b080eef4)
Solutions
- Query the worksheet directly: $sheet->hyperlinkExists($coord)
- Guard with $cell->getParent() !== null (or getWorksheetOrNull() !== null) before the call
- Store coordinate strings and refetch cells from a live sheet
Example fix
// before
$has = $cell->hasHyperlink(); // throws when detached
// after
$has = $sheet->hyperlinkExists('A1'); Defensive patterns
Strategy: validation
Validate before calling
if ($sheet !== null) {
$has = $sheet->hyperlinkExists($coord);
} elseif ($cell->getParent() !== null) {
$has = $cell->hasHyperlink();
} else {
$has = false;
} Type guard
function canQueryCellState(Cell $cell): bool
{
return $cell->getParent() !== null;
} Try / catch
null
Prevention
- Query hyperlinks via Worksheet::hyperlinkExists($coordinate) when possible
- Audit links while the workbook is still assembled, not from stale Cell references
- Guard any Cell method that needs a worksheet with a getParent() check first
When it happens
Trigger: Calling hasHyperlink() on a Cell retained after removeSheetByIndex(), unset($spreadsheet), or after the Cells collection was detached from its worksheet.
Common situations: Link-scraping/auditing passes that walk harvested Cell objects; serialization queues where Cell references outlive the workbook.
Related errors
- Cannot get hyperlink for cell that is not bound to a workshe
- Cannot set 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/7b326ba037647e6d.
Report an issue: GitHub.