PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception

Requested Print Area does not exist

Error message

Requested Print Area does not exist

What it means

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.

Source

Thrown at src/PhpSpreadsheet/Worksheet/PageSetup.php:591

    /**
     * Get print area.
     *
     * @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 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 65b080eef4)

Solutions

  1. Guard with isPrintAreaSet($index) === true before getPrintArea($index).
  2. Or count first: count(explode(',', $sheet->getPageSetup()->getPrintArea())) — remember the empty-string default.
  3. Use index 0 / no argument when you want the full print-area string.

Example fix

// before
$area = $sheet->getPageSetup()->getPrintArea(3); // only 2 ranges set

// after
$ps = $sheet->getPageSetup();
$areas = [];
for ($i = 1; $ps->isPrintAreaSet($i); ++$i) {
    $areas[] = $ps->getPrintArea($i);
}
Defensive patterns

Strategy: validation

Validate before calling

$ps = $sheet->getPageSetup();
$areas = [];
for ($i = 1; $ps->isPrintAreaSet($i); ++$i) {
    $areas[] = $ps->getPrintArea($i);
}

Try / catch

try {
    $area = $ps->getPrintArea($index);
} catch (PhpSpreadsheetException $e) {
    $area = $ps->getPrintArea(); // fall back to the whole print-area string
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17). Data as JSON: /api/errors/b028c107b67c12dc. Report an issue: GitHub.