moodle/moodle · error · PhpSpreadsheetException
Requested Print Area does not exist
Error message
Requested Print Area does not exist
What it means
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.
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.
Example fix
// before
$area = $sheet->getPageSetup()->getPrintArea(2); // throws if only 1 range
// after
$ps = $sheet->getPageSetup();
if ($ps->isPrintAreaSet()) {
$areas = explode(',', (string) $ps->getPrintArea());
$area = $areas[1] ?? $areas[0];
} Defensive patterns
Strategy: try-catch
Validate before calling
$ps = $sheet->getPageSetup();
$count = $ps->isPrintAreaSet() ? count(explode(',', (string) $ps->getPrintArea())) : 0;
if ($count < $index) { $index = 0; } Type guard
function hasPrintAreaIndex(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup $ps, int $i): bool { return $ps->isPrintAreaSet() && $i >= 1 && $i <= count(explode(',', (string) $ps->getPrintArea())); } Try / catch
try { $area = $ps->getPrintArea($i); } catch (\PhpOffice\PhpSpreadsheet\Exception $e) { $area = $ps->isPrintAreaSet() ? $ps->getPrintArea() : null; } Prevention
- Always call isPrintAreaSet() first
- Fetch all ranges with getPrintArea() and index the array yourself
- Never hardcode range indices
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Cell coordinate must be a range of cells.
- Cell coordinate must not be absolute.
- Cell coordinate must not specify a worksheet.
- Invalid index for setting print range.
- Invalid method for setting print range.
AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14).
Data as JSON: /api/errors/4b2f1ab09b84fb96.
Report an issue: GitHub.
Appendix: source
Thrown at public/lib/phpspreadsheet/phpspreadsheet/src/PhpSpreadsheet/Worksheet/PageSetup.php:593
/**
* Get print area.
*
* @param int $index Identifier for a specific print area range if several ranges have been set
* Default behaviour, or a index value of 0, will return all ranges as a comma-separated string
* Otherwise, the specific range identified by the value of $index will be returned
* Print areas are numbered from 1
*/
public function getPrintArea(int $index = 0): string
{
if ($index == 0) {
return (string) $this->printArea;
}
$printAreas = explode(',', (string) $this->printArea);
if (isset($printAreas[$index - 1])) {
return $printAreas[$index - 1];
}
throw new PhpSpreadsheetException('Requested Print Area does not exist');
}
/**
* Is print area set?
*
* @param int $index Identifier for a specific print area range if several ranges have been set
* Default behaviour, or an index value of 0, will identify whether any print range is set
* Otherwise, existence of the range identified by the value of $index will be returned
* Print areas are numbered from 1
*/
public function isPrintAreaSet(int $index = 0): bool
{
if ($index == 0) {
return $this->printArea !== null;
}
$printAreas = explode(',', (string) $this->printArea);
return isset($printAreas[$index - 1]);View on GitHub (pinned to 48ea8c3322)