{"record":{"id":"a7136325ecbc3221","repo":"PHPOffice/PhpSpreadsheet","slug":"invalid-method-for-setting-print-range","errorCode":null,"errorMessage":"Invalid method for setting print range.","messagePattern":"Invalid method for setting print range\\.","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Worksheet/PageSetup.php","lineNumber":699,"sourceCode":"                $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            }\n        } else {\n            throw new PhpSpreadsheetException('Invalid method for setting print range.');\n        }\n\n        return $this;\n    }\n\n    /**\n     * Add a new print area (e.g. 'A1:D10' or 'A1:D10,G5:M20') to the list of print areas.\n     *\n     * @param int $index Identifier for a specific print area range allowing several ranges to be set\n     *                            A positive index will insert after that indexed entry in the print areas list, while a\n     *                                negative index will insert before the indexed entry.\n     *                                Specifying an index value of 0, will always append the new print range at the end of the\n     *                                list.\n     *                            Print areas are numbered from 1\n     *\n     * @return $this\n     */\n    public function addPrintArea(string $value, int $index = -1): static","sourceCodeStart":681,"sourceCodeEnd":717,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Worksheet/PageSetup.php#L681-L717","documentation":"The $method argument of setPrintArea() accepts exactly two string constants: PageSetup::SETPRINTRANGE_OVERWRITE ('O') and PageSetup::SETPRINTRANGE_INSERT ('I'). The comparison is case-sensitive, so 'o', 'overwrite', 'insert', or any other string falls through both branches and throws 'Invalid method for setting print range.'","triggerScenarios":"setPrintArea('A1:D10', 0, 'insert') or 'X'; lowercase 'o'/'i'; a method string read from config or user input without validation.","commonSituations":"Passing full words instead of the single-letter constants; typo or case drift; API/config surfaces that forward arbitrary strings.","solutions":["Always pass the class constants: PageSetup::SETPRINTRANGE_OVERWRITE or PageSetup::SETPRINTRANGE_INSERT.","Normalize external input before use: map strtoupper(substr($method, 0, 1)) to 'O'/'I' or reject.","For simple appends, call addPrintArea() which supplies the correct method for you."],"exampleFix":"// before\n$ps->setPrintArea('A1:D10', 0, 'insert');\n\n// after\n$ps->setPrintArea('A1:D10', 0, PageSetup::SETPRINTRANGE_INSERT);","handlingStrategy":"type-guard","validationCode":"$method = strtoupper(substr((string) $method, 0, 1));\nif (!in_array($method, [PageSetup::SETPRINTRANGE_OVERWRITE, PageSetup::SETPRINTRANGE_INSERT], true)) {\n    $method = PageSetup::SETPRINTRANGE_OVERWRITE;\n}\n$ps->setPrintArea($range, $index, $method);","typeGuard":"function isValidPrintRangeMethod(string $method): bool\n{\n    return in_array($method, [PageSetup::SETPRINTRANGE_OVERWRITE, PageSetup::SETPRINTRANGE_INSERT], true);\n}","tryCatchPattern":null,"preventionTips":["Pass the constants PageSetup::SETPRINTRANGE_OVERWRITE ('O') / SETPRINTRANGE_INSERT ('I'), never literals","The check is case-sensitive — 'o' and 'i' are rejected","Use addPrintArea() for appends; it supplies the correct method itself"],"tags":["php","phpspreadsheet","page-setup","print-area","invalid-constant"],"backgroundTag":"invalid-enum-value","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}