{"record":{"id":"b028c107b67c12dc","repo":"PHPOffice/PhpSpreadsheet","slug":"requested-print-area-does-not-exist","errorCode":null,"errorMessage":"Requested Print Area does not exist","messagePattern":"Requested Print Area does not exist","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Worksheet/PageSetup.php","lineNumber":591,"sourceCode":"    /**\n     * Get print area.\n     *\n     * @param int $index Identifier for a specific print area range if several ranges have been set\n     *                            Default behaviour, or an index value of 0, will return all ranges as a comma-separated string\n     *                            Otherwise, the specific range identified by the value of $index will be returned\n     *                            Print areas are numbered from 1\n     */\n    public function getPrintArea(int $index = 0): string\n    {\n        if ($index == 0) {\n            return (string) $this->printArea;\n        }\n        $printAreas = explode(',', (string) $this->printArea);\n        if (isset($printAreas[$index - 1])) {\n            return $printAreas[$index - 1];\n        }\n\n        throw new PhpSpreadsheetException('Requested Print Area does not exist');\n    }\n\n    /**\n     * Is print area set?\n     *\n     * @param int $index Identifier for a specific print area range if several ranges have been set\n     *                            Default behaviour, or an index value of 0, will identify whether any print range is set\n     *                            Otherwise, existence of the range identified by the value of $index will be returned\n     *                            Print areas are numbered from 1\n     */\n    public function isPrintAreaSet(int $index = 0): bool\n    {\n        if ($index == 0) {\n            return $this->printArea !== null;\n        }\n        $printAreas = explode(',', (string) $this->printArea);\n\n        return isset($printAreas[$index - 1]);","sourceCodeStart":573,"sourceCodeEnd":609,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Worksheet/PageSetup.php#L573-L609","documentation":"A worksheet's print area is stored as one comma-joined string ('A1:D10,G5:M20'). getPrintArea($index) returns the whole string for index 0 and the Nth comma-separated range for N >= 1; if that slot does not exist it throws 'Requested Print Area does not exist'. One setPrintArea('A1:D10,G5:M20') call still creates two separately indexable entries.","triggerScenarios":"getPrintArea(3) when only two ranges exist; a hardcoded index left over from an earlier layout; calling with an index before any print area was set (getPrintArea() default is the empty string).","commonSituations":"Loops over print areas that assume a fixed count; mixing index semantics of isPrintAreaSet() (index 0 means 'any area set') with getPrintArea(); print areas edited between the count and the fetch.","solutions":["Guard with isPrintAreaSet($index) === true before getPrintArea($index).","Or count first: count(explode(',', $sheet->getPageSetup()->getPrintArea())) — remember the empty-string default.","Use index 0 / no argument when you want the full print-area string."],"exampleFix":"// before\n$area = $sheet->getPageSetup()->getPrintArea(3); // only 2 ranges set\n\n// after\n$ps = $sheet->getPageSetup();\n$areas = [];\nfor ($i = 1; $ps->isPrintAreaSet($i); ++$i) {\n    $areas[] = $ps->getPrintArea($i);\n}","handlingStrategy":"validation","validationCode":"$ps = $sheet->getPageSetup();\n$areas = [];\nfor ($i = 1; $ps->isPrintAreaSet($i); ++$i) {\n    $areas[] = $ps->getPrintArea($i);\n}","typeGuard":null,"tryCatchPattern":"try {\n    $area = $ps->getPrintArea($index);\n} catch (PhpSpreadsheetException $e) {\n    $area = $ps->getPrintArea(); // fall back to the whole print-area string\n}","preventionTips":["Always pair a non-zero index with isPrintAreaSet($index) === true","Remember one setPrintArea('A1:D10,G5:M20') call yields two indexable entries","Index 0 returns the full comma-joined string, not the first range"],"tags":["php","phpspreadsheet","page-setup","print-area","index-out-of-bounds"],"backgroundTag":"index-out-of-bounds","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}