{"record":{"id":"4b2f1ab09b84fb96","repo":"moodle/moodle","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":"PhpSpreadsheetException","httpStatus":null,"severity":"error","filePath":"public/lib/phpspreadsheet/phpspreadsheet/src/PhpSpreadsheet/Worksheet/PageSetup.php","lineNumber":593,"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 a 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":575,"sourceCodeEnd":611,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/lib/phpspreadsheet/phpspreadsheet/src/PhpSpreadsheet/Worksheet/PageSetup.php#L575-L611","documentation":"PageSetup::getPrintArea(int $index) throws when the requested print-area range (identified by 1-based $index) does not exist in the comma-separated printArea string. The method indexes into the exploded printArea list and throws rather than returning null, so callers can rely on a valid return value. It fires both when no print area is set at all and when $index exceeds the number of defined ranges.","triggerScenarios":"Calling getPrintArea(2) when only one range is defined (e.g. 'A1:D10'); calling getPrintArea(1) when setPrintArea() was never called; passing a negative or zero index.","commonSituations":"Iterating print areas with a hardcoded count; copying print settings between sheets where the source had multiple ranges and the target has fewer; loading templates whose print area was stripped by a reader.","solutions":["Check isPrintAreaSet() before calling getPrintArea().","Call getPrintArea() with no argument to fetch all ranges as one comma-separated string.","Wrap the call in try/catch on PhpSpreadsheetException and treat it as 'not set'.","Verify the number of ranges via count(explode(',', $pageSetup->getPrintArea())) before indexed access."],"exampleFix":"// before\n$area = $sheet->getPageSetup()->getPrintArea(2); // throws if only 1 range\n// after\n$ps = $sheet->getPageSetup();\nif ($ps->isPrintAreaSet()) {\n    $areas = explode(',', (string) $ps->getPrintArea());\n    $area = $areas[1] ?? $areas[0];\n}","handlingStrategy":"try-catch","validationCode":"$ps = $sheet->getPageSetup();\n$count = $ps->isPrintAreaSet() ? count(explode(',', (string) $ps->getPrintArea())) : 0;\nif ($count < $index) { $index = 0; }","typeGuard":"function hasPrintAreaIndex(\\PhpOffice\\PhpSpreadsheet\\Worksheet\\PageSetup $ps, int $i): bool { return $ps->isPrintAreaSet() && $i >= 1 && $i <= count(explode(',', (string) $ps->getPrintArea())); }","tryCatchPattern":"try { $area = $ps->getPrintArea($i); } catch (\\PhpOffice\\PhpSpreadsheet\\Exception $e) { $area = $ps->isPrintAreaSet() ? $ps->getPrintArea() : null; }","preventionTips":["Always call isPrintAreaSet() first","Fetch all ranges with getPrintArea() and index the array yourself","Never hardcode range indices"],"tags":["php","phpspreadsheet","page-setup","print-area"],"backgroundTag":"index-out-of-range","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}