{"record":{"id":"dd951f5be9f767f5","repo":"PHPOffice/PhpSpreadsheet","slug":"sheet-does-not-exist","errorCode":null,"errorMessage":"Sheet does not exist.","messagePattern":"Sheet does not exist\\.","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Spreadsheet.php","lineNumber":764,"sourceCode":"    }\n\n    /**\n     * Get index for sheet.\n     *\n     * @return int index\n     */\n    public function getIndex(Worksheet $worksheet, bool $noThrow = false): int\n    {\n        foreach ($this->workSheetCollection as $key => $value) {\n            if ($value === $worksheet) {\n                return $key;\n            }\n        }\n        if ($noThrow) {\n            return -1;\n        }\n\n        throw new Exception('Sheet does not exist.');\n    }\n\n    /**\n     * Set index for sheet by sheet name.\n     *\n     * @param string $worksheetName Sheet name to modify index for\n     * @param int $newIndexPosition New index for the sheet\n     *\n     * @return int New sheet index\n     */\n    public function setIndexByName(string $worksheetName, int $newIndexPosition): int\n    {\n        $oldIndex = $this->getIndex($this->getSheetByNameOrThrow($worksheetName));\n        $worksheet = array_splice(\n            $this->workSheetCollection,\n            $oldIndex,\n            1\n        );","sourceCodeStart":746,"sourceCodeEnd":782,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Spreadsheet.php#L746-L782","documentation":"Spreadsheet::getIndex(Worksheet $worksheet) walks the workbook's sheet collection looking for that exact object instance (=== comparison) and returns its position. If the instance is not one of the workbook's sheets, it throws 'Sheet does not exist.' The $noThrow flag (second argument, default false) makes it return -1 instead.","triggerScenarios":"Passing a Worksheet that belongs to a different Spreadsheet instance (e.g. built against another workbook before addExternalSheet()); passing a clone or a freshly constructed sheet that was never added; calling getIndex() on a sheet after removeSheetByIndex() removed it.","commonSituations":"Merging workbooks: taking $otherSheet from workbook B and asking workbook A for its index before/without re-parenting; helper functions that accept a Worksheet and assume it lives in the spreadsheet they also receive; code that copies sheets via clone and loses track of which workbook owns what.","solutions":["Pass true to get -1 instead of an exception: $index = $spreadsheet->getIndex($worksheet, true); then treat -1 as 'not present'.","Ensure the sheet is added to that workbook first (addSheet/addExternalSheet) before asking for its index.","When merging from another workbook, add the external sheet first, then call getIndex() on the returned/attached instance.","Look up by title instead of instance when identity is what matters: $spreadsheet->getSheetByName($title)."],"exampleFix":"// before\n$index = $workbookA->getIndex($sheetFromWorkbookB); // throws: Sheet does not exist.\n\n// after\n$added = $workbookA->addExternalSheet($sheetFromWorkbookB);\n$index = $workbookA->getIndex($added);\n// or non-throwing probe:\n$index = $workbookA->getIndex($maybeSheet, true);\nif ($index === -1) { /* not in this workbook */ }","handlingStrategy":"validation","validationCode":"$index = $spreadsheet->getIndex($worksheet, true); // -1 instead of exception\nif ($index === -1) {\n    // sheet not in this workbook: add it first or pick by title\n    $spreadsheet->addSheet($worksheet);\n    $index = $spreadsheet->getIndex($worksheet);\n}","typeGuard":"function worksheetBelongsTo(\\PhpOffice\\PhpSpreadsheet\\Spreadsheet $s, \\PhpOffice\\PhpSpreadsheet\\Worksheet\\Worksheet $w): bool\n{\n    return $s->getIndex($w, true) !== -1;\n}","tryCatchPattern":"try {\n    $index = $spreadsheet->getIndex($worksheet);\n} catch (\\PhpOffice\\PhpSpreadsheet\\Exception $e) {\n    $index = $spreadsheet->getIndex($spreadsheet->getSheetByName($worksheet->getTitle()) ?? $spreadsheet->getSheet(0));\n}","preventionTips":["Always add/attach a sheet to the workbook before asking for its index.","When probing, pass true as the second argument and handle -1.","Track which workbook owns each sheet in merge code."],"tags":["worksheet","identity","index-lookup","phpspreadsheet"],"backgroundTag":"worksheet-not-in-workbook","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}