{"record":{"id":"a2810aedb691e82e","repo":"PHPOffice/PhpSpreadsheet","slug":"cannot-get-row-when-cell-is-not-bound-to-a-workshe","errorCode":null,"errorMessage":"Cannot get row when cell is not bound to a worksheet","messagePattern":"Cannot get row when cell is not bound to a worksheet","errorType":"exception","errorClass":"SpreadsheetException","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Cell/Cell.php","lineNumber":149,"sourceCode":"    {\n        $parent = $this->parent;\n        if ($parent === null) {\n            throw new SpreadsheetException('Cannot get column when cell is not bound to a worksheet');\n        }\n\n        return $parent->getCurrentColumn();\n    }\n\n    /**\n     * Get cell coordinate row.\n     *\n     * @throws SpreadsheetException\n     */\n    public function getRow(): int\n    {\n        $parent = $this->parent;\n        if ($parent === null) {\n            throw new SpreadsheetException('Cannot get row when cell is not bound to a worksheet');\n        }\n\n        return $parent->getCurrentRow();\n    }\n\n    /**\n     * Get cell coordinate.\n     *\n     * @throws SpreadsheetException\n     */\n    public function getCoordinate(): string\n    {\n        $parent = $this->parent;\n        if ($parent !== null) {\n            $coordinate = $parent->getCurrentCoordinate();\n        } else {\n            $coordinate = null;\n        }","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Cell/Cell.php#L131-L167","documentation":"A Cell object is a thin wrapper whose $parent property links it to the Cells collection of a worksheet; getRow() simply delegates to $parent->getCurrentRow(). When the Cell is no longer attached to a collection (worksheet removed, collection emptied or detached, or the Cell object outlived it), $parent is null and this exception is thrown. The row number only exists as metadata owned by the live collection, not by the Cell itself.","triggerScenarios":"Calling $cell->getRow() on a Cell reference kept after $spreadsheet->removeSheetByIndex(), after unset($spreadsheet), after the cell collection was garbage-collected or emptied, or on a Cell obtained from a cloned/detached Cells collection.","commonSituations":"Caching Cell objects across import/export stages while the spreadsheet is torn down between stages; storing cells in queues or closures that execute after sheet teardown; copying cells between workbooks and then reusing the original references.","solutions":["Capture the row number (or the full coordinate string) as a plain int/string immediately after getCell(), instead of holding the Cell object","Guard the call with $cell->getParent() !== null (or $cell->getWorksheetOrNull() !== null) before getRow()","Re-fetch the cell from a live worksheet via $sheet->getCell($coordinate) when you need it later","Derive the row from a coordinate string you already hold using Coordinate::indexesFromString($coord)"],"exampleFix":"// before\n$cell = $sheet->getCell('B4');\n$spreadsheet->removeSheetByIndex($sheetIndex);\n$row = $cell->getRow(); // throws: cell no longer bound to a worksheet\n\n// after\n$cell = $sheet->getCell('B4');\n$row = $cell->getRow(); // capture while the worksheet is alive\n$coord = 'B4';\n$spreadsheet->removeSheetByIndex($sheetIndex);","handlingStrategy":"validation","validationCode":"if ($cell->getParent() === null) {\n    // cell is detached; recover from a live worksheet\n    $cell = $spreadsheet->getSheetByName($sheetName)->getCell($coord);\n}\n$row = $cell->getRow();","typeGuard":"function cellIsBoundToWorksheet(Cell $cell): bool\n{\n    return $cell->getParent() !== null && $cell->getParent()->getParent() !== null;\n}","tryCatchPattern":"try {\n    $row = $cell->getRow();\n} catch (SpreadsheetException $e) {\n    $cell = $sheet->getCell($coord); // re-bind via a live worksheet\n    $row = $cell->getRow();\n}","preventionTips":["Store coordinate strings or [row, col] ints across lifecycle boundaries, never Cell objects","Call getRow()/getCoordinate() immediately after getCell(), while the sheet is guaranteed alive","Do not remove sheets or unset the spreadsheet while Cell references are still in use"],"tags":["phpspreadsheet","cell","worksheet","detached-object","row"],"backgroundTag":"detached-object-access","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}