Skip to content

Novis\Spreadsheet\Sheet::addConditionalFormat

$sheet->addConditionalFormat(string $range, Conditional $rule): Sheet$sheet->addConditionalFormat(
    string $range,
    Conditional $rule,
): Sheet

Adds one conditional format to the cells of $range, and returns the sheet. Excel changes the look of each cell that matches the rule, and checks again when a value changes.

kind is "cell", "formula", "colorScale", "dataBar", "iconSet", "top", "bottom", "duplicates" or "unique". A "cell" rule compares each value with operator, such as "greater" or "between", and values. style is the Style of the cells that match. A color scale, a data bar and an icon set take colors or icons instead of a style.

Good to know: values are Excel formula text, so a text value has quotes, such as '"open"'. The rules of a sheet apply in the order you add them. A rule that needs a style and has none, or an unknown kind, throws a LogicError. A CSV file has no formats, so addConditionalFormat does nothing there.

The examples below show low stock in red, color the sales of each shop, and show the error for a rule without a style.

Parameters

  • $rangestring
  • $ruleConditional

Return value

Returns Sheet.

Changelog

VersionChange
0.0.6Each Novis\Spreadsheet\Sheet and Workbook method that adds to the file says what it adds, for example addRow(), addChart() and addSheet(). setPrintOptions() replaces print(). Chart->withSeries() and combinedWith() return a new chart.

Examples

Mark low stock in red

<?nvs
// Shows how to mark each quantity below 10 with a red fill.
use Novis\Spreadsheet\Style;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Stock');
$sheet->addRows([['item' => 'Pen', 'qty' => 120], ['item' => 'Ink', 'qty' => 3], ['item' => 'Pad', 'qty' => 60]]);
// Excel fills each cell of B2:B4 whose value is less than 10.
$sheet->addConditionalFormat($sheet->column('qty'), {
kind: 'cell',
operator: 'less',
values: ['10'],
style: Style::of({ fill: '#FFC7CE', color: '#9C0006' }),
});
bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// `Core\Zip::read` returns the XML of the sheet. This line checks that the rule is in it.
var $xml = Core\Zip::read($file, 'xl/worksheets/sheet1.xml') as string;
echo Core\Str::contains($xml, '<conditionalFormatting sqref="B2:B4">') ? 'B2:B4 has a rule' : 'no rule', "\n";
Output
B2:B4 has a rule

Colour the sales of each shop

<?nvs
// Shows how to colour sales from red for the lowest value to green for the highest.
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Sales');
$sheet->addRows([['shop' => 'North', 'sales' => 340], ['shop' => 'South', 'sales' => 210], ['shop' => 'East', 'sales' => 95]]);
// A colour scale takes two or three `colors` and no `style`.
$sheet->addConditionalFormat('B2:B4', { kind: 'colorScale', colors: ['#F8696B', '#63BE7B'] });
// A data bar draws a bar in each cell. Its length follows the value.
$sheet->addConditionalFormat('B2:B4', { kind: 'dataBar', colors: ['#638EC6'] });
bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// `Core\Zip::read` returns the XML of the sheet. These lines check that both rules are in it.
var $xml = Core\Zip::read($file, 'xl/worksheets/sheet1.xml') as string;
echo Core\Str::contains($xml, '<colorScale>') ? 'a colour scale' : 'no colour scale', "\n";
echo Core\Str::contains($xml, '<dataBar>') ? 'a data bar' : 'no data bar', "\n";
Output
a colour scale
a data bar

A rule without a style

<?nvs
// Shows the error for a rule that has no style, and for a kind that does not exist.
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Orders');
// A `cell` rule changes the look of a cell, so it needs a `style`.
try {
$sheet->addConditionalFormat('C2:C9', { kind: 'cell', operator: 'equal', values: ['"open"'] });
} catch (LogicError $e) {
echo $e->message, "\n";
}
try {
$sheet->addConditionalFormat('C2:C9', { kind: 'stars' });
} catch (LogicError $e) {
echo $e->message, "\n";
}
Output
The cell conditional format over `C2:C9` needs a `style` for the cells it formats.
The conditional format `kind` is `'cell'`, `'formula'`, `'colorScale'`, `'dataBar'`, `'iconSet'`, `'top'`, `'bottom'`, `'duplicates'` or `'unique'`. It has `'stars'`.