PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception

Text rotation {$angleInDegrees} should be a value between -9

Error message

Text rotation {$angleInDegrees} should be a value between -90 and 90.

What it means

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.

Source

Thrown at src/PhpSpreadsheet/Style/Alignment.php:368

     * @return $this
     */
    public function setTextRotation(int $angleInDegrees): static
    {
        // Excel2007 value 255 => PhpSpreadsheet value -165
        if ($angleInDegrees == self::TEXTROTATION_STACK_EXCEL) {
            $angleInDegrees = self::TEXTROTATION_STACK_PHPSPREADSHEET;
        }

        // Set rotation
        if (($angleInDegrees >= -90 && $angleInDegrees <= 90) || $angleInDegrees == self::TEXTROTATION_STACK_PHPSPREADSHEET) {
            if ($this->isSupervisor) {
                $styleArray = $this->getStyleArray(['textRotation' => $angleInDegrees]);
                $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
            } else {
                $this->textRotation = $angleInDegrees;
            }
        } else {
            throw new PhpSpreadsheetException("Text rotation $angleInDegrees should be a value between -90 and 90.");
        }

        return $this;
    }

    /**
     * Get Wrap Text.
     */
    public function getWrapText(): bool
    {
        if ($this->isSupervisor) {
            return $this->getSharedComponent()->getWrapText();
        }

        return $this->wrapText;
    }

    /**

View on GitHub (pinned to 65b080eef4)

Solutions

  1. Clamp to the supported range: $deg = max(-90, min(90, $deg)); before calling.
  2. Use Alignment::TEXTROTATION_STACK_EXCEL (255) for vertical stacked text instead of 90-plus values.
  3. 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.
  4. Note readers produce the internal -165 for stacked text — do not echo raw stored values back into setTextRotation().

Example fix

// before
$style->getAlignment()->setTextRotation(180); // throws

// after
use PhpOffice\PhpSpreadsheet\Style\Alignment;
$deg = max(-90, min(90, (int) $userAngle));
$style->getAlignment()->setTextRotation($deg);
// stacked vertical text instead of extreme angles:
$style->getAlignment()->setTextRotation(Alignment::TEXTROTATION_STACK_EXCEL);
Defensive patterns

Strategy: validation

Validate before calling

use PhpOffice\PhpSpreadsheet\Style\Alignment;
$deg = (int) $deg;
if ($deg !== Alignment::TEXTROTATION_STACK_EXCEL) {
    $deg = max(-90, min(90, $deg));
}
$alignment->setTextRotation($deg);

Type guard

function isValidTextRotation(int $deg): bool
{
    return ($deg >= -90 && $deg <= 90) || $deg === 255;
}

Try / catch

try {
    $style->getAlignment()->setTextRotation($deg);
} catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
    $style->getAlignment()->setTextRotation(0); // horizontal default
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


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