{"record":{"id":"b992bed7433f0337","repo":"PHPOffice/PhpSpreadsheet","slug":"sheet-does-not-have-a-parent","errorCode":null,"errorMessage":"Sheet does not have a parent.","messagePattern":"Sheet does not have a parent\\.","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Worksheet/Worksheet.php","lineNumber":850,"sourceCode":"\n    /**\n     * Get parent or null.\n     */\n    public function getParent(): ?Spreadsheet\n    {\n        return $this->parent;\n    }\n\n    /**\n     * Get parent, throw exception if null.\n     */\n    public function getParentOrThrow(): Spreadsheet\n    {\n        if ($this->parent !== null) {\n            return $this->parent;\n        }\n\n        throw new Exception('Sheet does not have a parent.');\n    }\n\n    /**\n     * Re-bind parent.\n     *\n     * @return $this\n     */\n    public function rebindParent(Spreadsheet $parent): static\n    {\n        if ($this->parent !== null) {\n            $definedNames = $this->parent->getDefinedNames();\n            foreach ($definedNames as $definedName) {\n                $parent->addDefinedName($definedName);\n            }\n\n            $this->parent->removeSheetByIndex(\n                $this->parent->getIndex($this)\n            );","sourceCodeStart":832,"sourceCodeEnd":868,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Worksheet/Worksheet.php#L832-L868","documentation":"Thrown by Worksheet::getParentOrThrow() when the worksheet's $parent (the owning Spreadsheet) is null — i.e. the sheet exists as a detached object. Many APIs (cross-sheet references, defined names, code-name de-duplication) need the workbook context, so they call getParentOrThrow() and fail fast on detached sheets.","triggerScenarios":"$sheet = new Worksheet(); (constructor parent omitted) followed by anything needing the workbook, e.g. $sheet->getCell('Other!A1') or a Table::setWorksheet() chain; sheets instantiated standalone in unit tests; sheet objects used after being detached from their spreadsheet.","commonSituations":"Instantiating Worksheet directly instead of through the spreadsheet in tests or helper code; copying a sheet into a new Spreadsheet without rebinding; forgetting that new Worksheet(null) leaves the sheet orphaned until addSheet().","solutions":["Create sheets through the workbook: $spreadsheet->createSheet() or $spreadsheet->addSheet(new Worksheet($spreadsheet))","Pass the parent at construction: new Worksheet($spreadsheet)","For an existing detached sheet, rebind: $sheet->rebindParent($spreadsheet) (works even from a null parent; it just skips defined-name migration)"],"exampleFix":"// before\n$sheet = new Worksheet();\n$sheet->getCell('Summary!A1'); // getParentOrThrow() throws\n\n// after\n$spreadsheet = new Spreadsheet();\n$sheet = $spreadsheet->createSheet();\n$sheet->getCell('Summary!A1');","handlingStrategy":"validation","validationCode":"if ($sheet->getParent() === null) {\n    $sheet->rebindParent($spreadsheet);\n    // or: $spreadsheet->addSheet($sheet);\n}\n// safe to use workbook-dependent APIs now","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always create worksheets via $spreadsheet->createSheet() or new Worksheet($spreadsheet)","In tests, build a real Spreadsheet fixture instead of bare Worksheet objects","Assert getParent() !== null at the top of helpers that resolve cross-sheet references"],"tags":["phpspreadsheet","worksheet","spreadsheet","detached-object","null-parent"],"backgroundTag":"missing-parent-context","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}