PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception

Style is not a conditional style

Error message

Style is not a conditional style

What it means

Worksheet::duplicateConditionalStyle(array $styles, string $range) requires every element of $styles to be an instance of PhpSpreadsheet\Worksheet\Conditional (a conditional formatting rule: condition type/operator plus its style). The loop validates each element and throws on the first non-Conditional entry — regular Style objects or plain arrays are not accepted.

Source

Thrown at src/PhpSpreadsheet/Worksheet/Worksheet.php:1712

        return $this;
    }

    /**
     * Duplicate conditional style to a range of cells.
     *
     * Please note that this will overwrite existing cell styles for cells in range!
     *
     * @param Conditional[] $styles Cell style to duplicate
     * @param string $range Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1")
     *
     * @return $this
     */
    public function duplicateConditionalStyle(array $styles, string $range = ''): static
    {
        foreach ($styles as $cellStyle) {
            if (!($cellStyle instanceof Conditional)) {
                throw new Exception('Style is not a conditional style');
            }
        }

        // Calculate range outer borders
        [$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($range . ':' . $range);

        // Make sure we can loop upwards on rows and columns
        if ($rangeStart[0] > $rangeEnd[0] && $rangeStart[1] > $rangeEnd[1]) {
            $tmp = $rangeStart;
            $rangeStart = $rangeEnd;
            $rangeEnd = $tmp;
        }

        // Loop through cells and apply styles
        for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
            for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
                $this->setConditionalStyles(Coordinate::stringFromColumnIndex($col) . $row, $styles);
            }

View on GitHub (pinned to 65b080eef4)

Solutions

  1. Build Conditional objects: (new Conditional())->setConditionType(Conditional::CONDITION_CELLIS)->setOperatorType(Conditional::OPERATOR_GREATERTHAN)->addCondition(10), style via $conditional->getStyle()->applyFromArray([...]), then pass [$conditional]
  2. Filter/assert before the call: array_filter($styles, fn ($s) => $s instanceof Conditional) — or better, fail loudly if the filter removes anything
  3. For plain-cell (non-conditional) formatting use duplicateStyle() instead

Example fix

// before
$sheet->duplicateConditionalStyle([$sheet->getStyle('A1')], 'B2:B10'); // Style is not Conditional

// after
$conditional = new Conditional();
$conditional->setConditionType(Conditional::CONDITION_CELLIS)
    ->setOperatorType(Conditional::OPERATOR_GREATERTHAN)
    ->addCondition(10);
$conditional->getStyle()->applyFromArray(['font' => ['bold' => true]]);
$sheet->duplicateConditionalStyle([$conditional], 'B2:B10');
Defensive patterns

Strategy: type-guard

Validate before calling

use PhpOffice\PhpSpreadsheet\Worksheet\Conditional;

$invalid = array_filter($styles, fn ($s) => !$s instanceof Conditional);
if ($invalid !== []) {
    throw new InvalidArgumentException('styles must all be Conditional instances');
}
$sheet->duplicateConditionalStyle($styles, 'B2:B10');

Type guard

use PhpOffice\PhpSpreadsheet\Worksheet\Conditional;

/** @param mixed[] $styles */
function allConditional(array $styles): bool
{
    return array_all($styles, fn ($s) => $s instanceof Conditional);
    // PHP < 8.2: !in_array(false, array_map(fn ($s) => $s instanceof Conditional, $styles), true)
}

Prevention

When it happens

Trigger: $sheet->duplicateConditionalStyle([$sheet->getStyle('A1')], 'B2:B10') — passing a Style from getStyle(); passing the output of getConditionalStyles() mixed with raw arrays; passing [['operator' => '>', 'value' => 5]] style config arrays.

Common situations: Confusing duplicateStyle() (regular styles) with duplicateConditionalStyle(); serializing conditional rules to arrays for storage and passing them back unhydrated; copy-pasting config-array examples that predate the object API.

Related errors


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