PHPOffice/PhpSpreadsheet · error · SpreadsheetException

Cannot get column when cell is not bound to a worksheet

Error message

Cannot get column when cell is not bound to a worksheet

What it means

Cell::getColumn() delegates to the parent Cells collection (getCurrentColumn). After detach() (or once the worksheet/collection is gone) $this->parent is null and it throws PhpOffice\PhpSpreadsheet\Exception 'Cannot get column when cell is not bound to a worksheet'. The sibling getRow() throws the analogous row message. Any cached Cell object whose collection was evicted/rebuilt will fail the same way.

Source

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

        } else {
            $valueBinder = $worksheet->getParent()?->getValueBinder() ?? self::getValueBinder();
            if ($valueBinder->bindValue($this, $value) === false) {
                throw new SpreadsheetException('Value could not be bound to cell.');
            }
        }
        $this->ignoredErrors = new IgnoredErrors();
    }

    /**
     * Get cell coordinate column.
     *
     * @throws SpreadsheetException
     */
    public function getColumn(): string
    {
        $parent = $this->parent;
        if ($parent === null) {
            throw new SpreadsheetException('Cannot get column when cell is not bound to a worksheet');
        }

        return $parent->getCurrentColumn();
    }

    /**
     * Get cell coordinate row.
     *
     * @throws SpreadsheetException
     */
    public function getRow(): int
    {
        $parent = $this->parent;
        if ($parent === null) {
            throw new SpreadsheetException('Cannot get row when cell is not bound to a worksheet');
        }

        return $parent->getCurrentRow();

View on GitHub (pinned to 65b080eef4)

Solutions

  1. Read the coordinate once, immediately: use $cell->getCoordinate() at fetch time (or the coordinate you fetched by) and carry the string, not the object
  2. Always re-acquire cells via $sheet->getCell('A1') at point of use instead of holding objects
  3. Complete all cell processing before removing/rebuilding worksheets
  4. Wrap in try/catch PhpOffice\PhpSpreadsheet\Exception if detaching is possible, and re-fetch on failure

Example fix

// before: object outlives its collection
$cell = $sheet->getCell('A1');
// ... worksheet rebuilt ...
$col = $cell->getColumn(); // throws

// after: capture primitives early
$coord = $sheet->getCell('A1')->getCoordinate(); // 'A1'
// later
$col = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString(substr($coord, 0, strlen($coord) - 1)); // or re-fetch via $sheet->getCell($coord)
Defensive patterns

Strategy: try-catch

Validate before calling

// Capture the coordinate string while the cell is certainly live
$coord = $cell->getCoordinate();
// ... later, defensively:
try {
    $col = $cell->getColumn();
} catch (\PhpOffice\PhpSpreadsheet\Exception) {
    $cell = $sheet->getCell($coord); // re-attach by re-fetching
    $col = $cell->getColumn();
}

Type guard

function cellStillBound(\PhpOffice\PhpSpreadsheet\Cell\Cell $cell): bool
{
    try {
        $cell->getColumn();
        return true;
    } catch (\PhpOffice\PhpSpreadsheet\Exception) {
        return false;
    }
}

Try / catch

try {
    $col = $cell->getColumn();
    $row = $cell->getRow();
} catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
    if (str_contains($e->getMessage(), 'not bound to a worksheet')) {
        [$col, $row] = [Coordinate::columnIndexFromString(preg_replace('/\d+/', '', $coord)), (int) preg_replace('/^\D+/', '', $coord)];
    } else { throw $e; }
}

Prevention

When it happens

Trigger: $cell->detach(); $cell->getColumn(); storing Cell instances and calling getColumn()/getRow()/getCoordinate() after the worksheet was removed or its collection replaced; iterating cells then deleting the sheet mid-loop; long-running jobs where cell garbage collection detached the object between fetch and use.

Common situations: Caching Cell objects for speed in report builders; passing Cell objects between components while the workbook is restructured; memory-constrained workloads with aggressive collection caching that detaches cells; code written against old versions that tolerated detached access.

Related errors


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