{"record":{"id":"f97fde5ffbec7bb9","repo":"PHPOffice/PhpSpreadsheet","slug":"text-rotation-angleindegrees-should-be-a-value","errorCode":null,"errorMessage":"Text rotation {$angleInDegrees} should be a value between -90 and 90.","messagePattern":"Text rotation (.+?) should be a value between -90 and 90\\.","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Style/Alignment.php","lineNumber":368,"sourceCode":"     * @return $this\n     */\n    public function setTextRotation(int $angleInDegrees): static\n    {\n        // Excel2007 value 255 => PhpSpreadsheet value -165\n        if ($angleInDegrees == self::TEXTROTATION_STACK_EXCEL) {\n            $angleInDegrees = self::TEXTROTATION_STACK_PHPSPREADSHEET;\n        }\n\n        // Set rotation\n        if (($angleInDegrees >= -90 && $angleInDegrees <= 90) || $angleInDegrees == self::TEXTROTATION_STACK_PHPSPREADSHEET) {\n            if ($this->isSupervisor) {\n                $styleArray = $this->getStyleArray(['textRotation' => $angleInDegrees]);\n                $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);\n            } else {\n                $this->textRotation = $angleInDegrees;\n            }\n        } else {\n            throw new PhpSpreadsheetException(\"Text rotation $angleInDegrees should be a value between -90 and 90.\");\n        }\n\n        return $this;\n    }\n\n    /**\n     * Get Wrap Text.\n     */\n    public function getWrapText(): bool\n    {\n        if ($this->isSupervisor) {\n            return $this->getSharedComponent()->getWrapText();\n        }\n\n        return $this->wrapText;\n    }\n\n    /**","sourceCodeStart":350,"sourceCodeEnd":386,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Style/Alignment.php#L350-L386","documentation":"Alignment::setTextRotation() accepts degrees between -90 and 90 (counterclockwise/clockwise text rotation), plus the special value Alignment::TEXTROTATION_STACK_EXCEL (255) meaning vertically stacked characters, which the setter internally converts to TEXTROTATION_STACK_PHPSPREADSHEET (-165). Any other integer throws, because Excel's cell format has no representation for it.","triggerScenarios":"setTextRotation(180) or setTextRotation(270) trying to flip text upside down; values like 100 or -95 from user input; rotations read from another format (e.g. HTML/CSS degrees) passed through unclamped; floats like 45.5 surviving an (int) cast to a valid value but 90.5 becoming 90 (fine) while 180.5 becomes 180 (throws).","commonSituations":"Porting CSS transform rotate() values into Excel exports; user-facing 'angle' fields without bounds; assuming Excel supports arbitrary angles like 180°; feeding rotation values from image/PDF libraries with wider ranges.","solutions":["Clamp to the supported range: $deg = max(-90, min(90, $deg)); before calling.","Use Alignment::TEXTROTATION_STACK_EXCEL (255) for vertical stacked text instead of 90-plus values.","Reject/normalize user input outside -90..90 early with a validation message; map '180' to a flipped-text workaround (e.g. stacked text or a font trick) if truly needed.","Note readers produce the internal -165 for stacked text — do not echo raw stored values back into setTextRotation()."],"exampleFix":"// before\n$style->getAlignment()->setTextRotation(180); // throws\n\n// after\nuse PhpOffice\\PhpSpreadsheet\\Style\\Alignment;\n$deg = max(-90, min(90, (int) $userAngle));\n$style->getAlignment()->setTextRotation($deg);\n// stacked vertical text instead of extreme angles:\n$style->getAlignment()->setTextRotation(Alignment::TEXTROTATION_STACK_EXCEL);","handlingStrategy":"validation","validationCode":"use PhpOffice\\PhpSpreadsheet\\Style\\Alignment;\n$deg = (int) $deg;\nif ($deg !== Alignment::TEXTROTATION_STACK_EXCEL) {\n    $deg = max(-90, min(90, $deg));\n}\n$alignment->setTextRotation($deg);","typeGuard":"function isValidTextRotation(int $deg): bool\n{\n    return ($deg >= -90 && $deg <= 90) || $deg === 255;\n}","tryCatchPattern":"try {\n    $style->getAlignment()->setTextRotation($deg);\n} catch (\\PhpOffice\\PhpSpreadsheet\\Exception $e) {\n    $style->getAlignment()->setTextRotation(0); // horizontal default\n}","preventionTips":["Clamp user angles to -90..90 before styling cells.","Use 255 (TEXTROTATION_STACK_EXCEL) for stacked vertical text.","Do not feed stored internal values (-165) back into the setter."],"tags":["styles","alignment","text-rotation","validation","range","phpspreadsheet"],"backgroundTag":"value-out-of-range","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}