Range rule

Apply a conditional format rule to an A1 range. Each cell in the range is evaluated independently.

Methods

conditionalFormatRange(string $rangeA1, \Vtiful\Kernel\ConditionalFormat $cf): self

string $rangeA1

Target range in A1 notation, e.g. "A2:A10" or "B2:D20".

\Vtiful\Kernel\ConditionalFormat $cf

A rule object built with \Vtiful\Kernel\ConditionalFormat.

Greater-than across a range

Mark every cell in A2:A10 greater than 50 with a red background and white font:

$config = ['path' => './tests'];
$excel  = new \Vtiful\Kernel\Excel($config);

$fileObject = $excel->fileName('tutorial.xlsx');
$fileHandle = $fileObject->getHandle();

$highlight = (new \Vtiful\Kernel\Format($fileHandle))
    ->background(\Vtiful\Kernel\Format::COLOR_RED)
    ->fontColor(\Vtiful\Kernel\Format::COLOR_WHITE)
    ->toResource();

$cf = new \Vtiful\Kernel\ConditionalFormat();
$cf->type(\Vtiful\Kernel\ConditionalFormat::TYPE_CELL)
   ->criteria(\Vtiful\Kernel\ConditionalFormat::CRITERIA_GREATER_THAN)
   ->value(50)
   ->format($highlight);

$fileObject->header(['score'])
    ->data([[10], [40], [55], [60], [70], [80], [90], [100], [120]])
    ->conditionalFormatRange('A2:A10', $cf)
    ->output();

Two-colour scale

Apply a white-to-green gradient across A2:A10:

Three-colour scale

Low / mid / high red-yellow-green gradient:

Formula rule

Match cells via an arbitrary Excel formula (here: highlight even rows):

Last updated