PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception
Cell coordinate must be a range of cells.
Error message
Cell coordinate must be a range of cells.
What it means
setPrintArea() validates that the value contains a colon, i.e. is a range rather than a single cell. Passing 'A1' (or any malformed string without ':') throws 'Cell coordinate must be a range of cells.' To print a single cell you must spell it as the degenerate range 'A1:A1'.
Source
Thrown at src/PhpSpreadsheet/Worksheet/PageSetup.php:661
* 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;
}
if (($index <= 0) || ($index > count($printAreas))) {
throw new PhpSpreadsheetException('Invalid index for setting print range.');View on GitHub (pinned to 65b080eef4)
Solutions
- Pass 'A1:A1' when you genuinely want a single cell.
- Always assemble ranges as $start . ':' . $end in your code.
- Validate with str_contains($value, ':') before calling.
Example fix
// before
$sheet->getPageSetup()->setPrintArea('A1');
// after
$sheet->getPageSetup()->setPrintArea('A1:A1'); Defensive patterns
Strategy: validation
Validate before calling
if (!str_contains($value, ':')) {
$value = $value . ':' . $value; // single cell -> degenerate range 'A1:A1'
}
$sheet->getPageSetup()->setPrintArea($value); Type guard
function isBareCellRange(string $value): bool
{
return !str_contains($value, '!') && str_contains($value, ':') && !str_contains($value, '$');
} Prevention
- A single cell must be spelled 'A1:A1'
- Assemble ranges in code as $start . ':' . $end, never from loose fragments
- Check str_contains($value, ':') before calling setPrintArea()
When it happens
Trigger: setPrintArea('A1'); a dynamically built range where start equals end and the colon got dropped; typos like 'A1..E5' or 'A1;E5'; feeding a cell coordinate where a range is required.
Common situations: Print areas computed from data extents that collapse to one cell; coordinates obtained from getCoordinate()-style APIs passed to setPrintArea; separators localized by spreadsheet UIs.
Related errors
- Requested Print Area does not exist
- Cell coordinate must not specify a worksheet.
- Cell coordinate must not be absolute.
- Invalid index for setting print range.
- Invalid method for setting print range.
AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17).
Data as JSON: /api/errors/65c6f3b39946d615.
Report an issue: GitHub.