{"record":{"id":"594b03c906fbe276","repo":"PHPOffice/PhpSpreadsheet","slug":"your-requested-sheet-index-sheetindex-is-out-o","errorCode":null,"errorMessage":"Your requested sheet index: {$sheetIndex} is out of bounds. The actual number of sheets is {$numSheets}.","messagePattern":"Your requested sheet index: (.+?) is out of bounds\\. The actual number of sheets is (.+?)\\.","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Spreadsheet.php","lineNumber":700,"sourceCode":"        if (\n            ($this->activeSheetIndex >= $sheetIndex)\n            && ($this->activeSheetIndex > 0 || $numSheets <= 1)\n        ) {\n            --$this->activeSheetIndex;\n        }\n    }\n\n    /**\n     * Get sheet by index.\n     *\n     * @param int $sheetIndex Sheet index\n     */\n    public function getSheet(int $sheetIndex): Worksheet\n    {\n        if (!isset($this->workSheetCollection[$sheetIndex])) {\n            $numSheets = $this->getSheetCount();\n\n            throw new Exception(\n                \"Your requested sheet index: {$sheetIndex} is out of bounds. The actual number of sheets is {$numSheets}.\"\n            );\n        }\n\n        return $this->workSheetCollection[$sheetIndex];\n    }\n\n    /**\n     * Get all sheets.\n     *\n     * @return Worksheet[]\n     */\n    public function getAllSheets(): array\n    {\n        return $this->workSheetCollection;\n    }\n\n    /**","sourceCodeStart":682,"sourceCodeEnd":718,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Spreadsheet.php#L682-L718","documentation":"Spreadsheet::getSheet(int $index) returns the worksheet at a 0-based position and throws when no sheet is stored at that offset (isset() on the collection fails). The message echoes the requested index and the actual sheet count. It is the standard guard for positional access to the sheet collection.","triggerScenarios":"getSheet(1) on a workbook with a single sheet; getSheet($i) inside a for loop bounded by a stale count taken before sheets were removed; getSheet($n) where $n comes from user input or a config default that assumes a template layout which the loaded file does not have.","commonSituations":"Reports that assume a fixed template ('data goes on sheet 2') opened against a user-supplied file with fewer tabs; iterating sheets with a cached count; code written against a fixture workbook then run on trimmed-down production files.","solutions":["Guard the call: if ($index >= 0 && $index < $spreadsheet->getSheetCount()) { $sheet = $spreadsheet->getSheet($index); }","Iterate safely over all sheets: foreach ($spreadsheet->getSheets() as $sheet) { ... } instead of indexed loops.","Fetch by name when possible: $spreadsheet->getSheetByNameOrThrow('Data').","Re-count inside loops that add or remove sheets: use $spreadsheet->getSheetCount() at each iteration."],"exampleFix":"// before\n$sheet = $spreadsheet->getSheet(2); // throws: index out of bounds\n\n// after\n$sheet = $spreadsheet->getSheetCount() > 2\n    ? $spreadsheet->getSheet(2)\n    : $spreadsheet->getSheetByNameOrThrow('Data');","handlingStrategy":"validation","validationCode":"if ($index >= 0 && $index < $spreadsheet->getSheetCount()) {\n    $sheet = $spreadsheet->getSheet($index);\n}","typeGuard":"function sheetIndexExists(\\PhpOffice\\PhpSpreadsheet\\Spreadsheet $s, int $i): bool\n{\n    return $i >= 0 && $i < $s->getSheetCount();\n}","tryCatchPattern":"try {\n    $sheet = $spreadsheet->getSheet($index);\n} catch (\\PhpOffice\\PhpSpreadsheet\\Exception $e) {\n    $sheet = $spreadsheet->getSheetByNameOrThrow($spreadsheet->getSheetNames()[0]);\n}","preventionTips":["Iterate with foreach ($spreadsheet->getSheets() as $sheet) instead of indexed for loops.","Validate file shape (expected tabs) right after loading.","Prefer name-based access when the target is known by title."],"tags":["worksheet","index-out-of-bounds","getsheet","phpspreadsheet"],"backgroundTag":"index-out-of-bounds","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}