{"record":{"id":"6b28871fe6686c3c","repo":"PHPOffice/PhpSpreadsheet","slug":"invalid-index-for-setting-print-range","errorCode":null,"errorMessage":"Invalid index for setting print range.","messagePattern":"Invalid index for setting print range\\.","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Worksheet/PageSetup.php","lineNumber":679,"sourceCode":"            throw new PhpSpreadsheetException('Cell coordinate must be a range of cells.');\n        } elseif (str_contains($value, '$')) {\n            throw new PhpSpreadsheetException('Cell coordinate must not be absolute.');\n        }\n        $value = strtoupper($value);\n        if (!$this->printArea) {\n            $index = 0;\n        }\n\n        if ($method == self::SETPRINTRANGE_OVERWRITE) {\n            if ($index == 0) {\n                $this->printArea = $value;\n            } else {\n                $printAreas = explode(',', (string) $this->printArea);\n                if ($index < 0) {\n                    $index = count($printAreas) - abs($index) + 1;\n                }\n                if (($index <= 0) || ($index > count($printAreas))) {\n                    throw new PhpSpreadsheetException('Invalid index for setting print range.');\n                }\n                $printAreas[$index - 1] = $value;\n                $this->printArea = implode(',', $printAreas);\n            }\n        } elseif ($method == self::SETPRINTRANGE_INSERT) {\n            if ($index == 0) {\n                $this->printArea = $this->printArea ? ($this->printArea . ',' . $value) : $value;\n            } else {\n                $printAreas = explode(',', (string) $this->printArea);\n                if ($index < 0) {\n                    $index = (int) abs($index) - 1;\n                }\n                if ($index > count($printAreas)) {\n                    throw new PhpSpreadsheetException('Invalid index for setting print range.');\n                }\n                $printAreas = array_merge(array_slice($printAreas, 0, $index), [$value], array_slice($printAreas, $index));\n                $this->printArea = implode(',', $printAreas);\n            }","sourceCodeStart":661,"sourceCodeEnd":697,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Worksheet/PageSetup.php#L661-L697","documentation":"With method 'O' (overwrite, the default), setPrintArea($value, $index) replaces the Nth existing range. When no print area exists yet, any index is coerced to 0; otherwise a negative index is converted (count - abs(index) + 1) and the final index must land in 1..count, or 'Invalid index for setting print range.' is thrown.","triggerScenarios":"setPrintArea('A1:D10', 5) when only 2 ranges exist; setPrintArea('A1:D10', -10) with 2 ranges (2 - 10 + 1 = -7, below 1); assuming out-of-range positive indexes append — they do not, index 0 does.","commonSituations":"Index values taken from user input or config; list sizes that changed between versions; negative-index arithmetic misjudged (it counts from the end).","solutions":["Use index 0 to replace the entire print area instead of a specific slot.","Count existing ranges first and clamp: $count = count(explode(',', $ps->getPrintArea())); keep 1 <= $index <= $count.","For negative indexes, verify count - abs($index) + 1 lands within 1..count before calling."],"exampleFix":"// before\n$ps->setPrintArea('A1:D10', 5); // only 2 ranges exist\n\n// after\n$count = $ps->getPrintArea() === '' ? 0 : count(explode(',', $ps->getPrintArea()));\n$ps->setPrintArea('A1:D10', $index >= 1 && $index <= $count ? $index : 0);","handlingStrategy":"validation","validationCode":"$ps = $sheet->getPageSetup();\n$count = $ps->getPrintArea() === '' ? 0 : count(explode(',', $ps->getPrintArea()));\nif ($index !== 0 && ($index < 1 || $index > $count)) {\n    $index = 0; // overwrite the whole print area instead of a specific slot\n}\n$ps->setPrintArea($range, $index, PageSetup::SETPRINTRANGE_OVERWRITE);","typeGuard":null,"tryCatchPattern":"try {\n    $ps->setPrintArea($range, $index);\n} catch (PhpSpreadsheetException $e) {\n    $ps->setPrintArea($range); // fall back to full overwrite (index 0)\n}","preventionTips":["Index 0 replaces the whole print area — prefer it over positional slots","Negative indexes convert to count - abs(n) + 1 and must still land in 1..count","With no existing print area any index is coerced to 0, hiding bad values until one exists"],"tags":["php","phpspreadsheet","page-setup","print-area","index-out-of-bounds"],"backgroundTag":"index-out-of-bounds","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}