PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception

Cell coordinate must not specify a worksheet.

Error message

Cell coordinate must not specify a worksheet.

What it means

setPrintArea() expects a bare range for the current worksheet ('A1:E5' or several comma-separated ranges). An exclamation mark in the value means a sheet-qualified coordinate ('Sheet1!A1:E5') was passed, which the method rejects before doing anything else. The sheet is implied — the PageSetup object already belongs to it.

Source

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

     *                            When the method is "O"verwrite, then a positive integer index will overwrite that indexed
     *                                entry in the print areas list; a negative index value will identify which entry to
     *                                overwrite working backward through the print area to the list, with the last entry as -1.
     *                                Specifying an index value of 0, will overwrite <b>all</b> existing print ranges.
     *                            When the method is "I"nsert, then a positive index will insert after that indexed entry in
     *                                the print areas list, while a negative index will insert before the indexed entry.
     *                                Specifying an index value of 0, will always append the new print range at the end of the
     *                                list.
     *                            Print areas are numbered from 1
     * @param string $method Determines the method used when setting multiple print areas
     *                            Default behaviour, or the "O" method, overwrites existing print area
     *                            The "I" method, inserts the new print area before any specified index, or at the end of the list
     *
     * @return $this
     */
    public function setPrintArea(string $value, int $index = 0, string $method = self::SETPRINTRANGE_OVERWRITE): static
    {
        if (str_contains($value, '!')) {
            throw new PhpSpreadsheetException('Cell coordinate must not specify a worksheet.');
        } elseif (!str_contains($value, ':')) {
            throw new PhpSpreadsheetException('Cell coordinate must be a range of cells.');
        } elseif (str_contains($value, '$')) {
            throw new PhpSpreadsheetException('Cell coordinate must not be absolute.');
        }
        $value = strtoupper($value);
        if (!$this->printArea) {
            $index = 0;
        }

        if ($method == self::SETPRINTRANGE_OVERWRITE) {
            if ($index == 0) {
                $this->printArea = $value;
            } else {
                $printAreas = explode(',', (string) $this->printArea);
                if ($index < 0) {
                    $index = count($printAreas) - abs($index) + 1;
                }

View on GitHub (pinned to 65b080eef4)

Solutions

  1. Strip the sheet prefix: $value = substr($value, strrpos($value, '!') + 1);
  2. Build print ranges from bare cell coordinates only.
  3. Reject sheet-qualified strings at your input boundary with str_contains($value, '!').

Example fix

// before
$sheet->getPageSetup()->setPrintArea('Report!A1:E5');

// after
$value = 'Report!A1:E5';
$value = substr($value, strrpos($value, '!') + 1);
$sheet->getPageSetup()->setPrintArea($value);
Defensive patterns

Strategy: validation

Validate before calling

if (str_contains($value, '!')) {
    $value = substr($value, strrpos($value, '!') + 1); // drop the sheet prefix
}
$sheet->getPageSetup()->setPrintArea($value);

Type guard

function isBareCellRange(string $value): bool
{
    return !str_contains($value, '!') && str_contains($value, ':') && !str_contains($value, '$');
}

Prevention

When it happens

Trigger: setPrintArea('Sheet1!A1:E5'); ranges copy-pasted from the formula bar or Name Manager; concatenating $sheet->getTitle() . '!' . $range; reusing strings meant for defined names or print titles.

Common situations: Ranges round-tripped through Excel UI or other libraries that always emit sheet-qualified references; code migrated from fluent coordinate builders.

Related errors


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