Skip to content

Novis\Spreadsheet\Style::of

Makes a style for cells: the font, the colors, the borders, the alignment and the number format.

Every field is optional. A field you leave out comes from the row, then from the column, and then from Excel's default. bold, italic and wrap are true or false. color and fill are colors such as "#FF0000". border is a line such as "thin", for every side or for each side on its own. format is a number format, such as "0.00", or a short name such as "money", "percent" or "date".

Give a style to a cell with Cell::of, to a row with the style option of addRow, or to a column with columns. Two styles with the same fields are one style in the file.

Good to know: a value that is not allowed, such as border: "wavy", throws a LogicError with the values that are allowed. A CSV file has no styles, so they are left out.

Parameters

  • $fieldsFields

Return value

Returns Style.

Examples

A bold totals row

<?nvs
// Shows how to make the totals row of a report bold, with a line above it.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Style;
use Novis\Spreadsheet\Workbook;
// `border` with one side draws a line on that side only.
var $totals = Style::of({ bold: true, border: { top: 'thin' }, fill: '#EEEEEE' });
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Orders');
$sheet->addRows([['item' => 'Pen', 'total' => 3.0], ['item' => 'Ink', 'total' => 4.25]]);
// The `style` option of `addRow` gives the style to every cell of the row.
$sheet->addRow(['item' => 'Total', 'total' => 7.25], { style: $totals });
bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
foreach (Reader::open($file)->rows('Orders', { header: true }) as var $row) {
echo $row->values['item'], ': ', $row->values['total'], "\n";
}
echo 'bold: ', $totals->fields->bold == true ? 'yes' : 'no', "\n";
Output
Pen: 3
Ink: 4.25
Total: 7.25
bold: yes

A price column with a money format

<?nvs
// Shows how to show the prices of a column with two decimals and a thousands separator.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Style;
use Novis\Spreadsheet\Workbook;
// `money` is a short name for the format `#,##0.00`. `align` moves the text to the right.
var $money = Style::of({ format: 'money', align: 'right' });
bytes $file = Workbook::fromRows(
[['item' => 'Desk', 'price' => 1249.5], ['item' => 'Lamp', 'price' => 35.0]],
{ columns: ['price' => { style: $money }] },
)->bytes();
// The cell keeps the number. `text: true` reads each cell as Excel shows it.
foreach (Reader::open($file)->rows('Sheet1', { header: true, text: true }) as var $row) {
echo $row->values['item'], ': ', $row->values['price'], "\n";
}
Output
Desk: 1,249.50
Lamp: 35.00

A value that is not allowed

<?nvs
// Shows what happens when a style field has a value that is not allowed.
use Novis\Spreadsheet\Style;
// `border` accepts six names. Any other name throws a `LogicError` that lists them.
try {
Style::of({ border: 'wavy' });
} catch (LogicError $e) {
echo $e->message, "\n";
}
// `align` works the same way.
try {
Style::of({ align: 'middle' });
} catch (LogicError $e) {
echo $e->message, "\n";
}
// Two styles with the same fields have the same `key`, so the file has one entry for both.
echo Style::of({ bold: true })->key == Style::of({ bold: true })->key ? 'same style' : 'two styles', "\n";
Output
The style field `border` is `'thin'`, `'medium'`, `'thick'`, `'dashed'`, `'dotted'` or `'double'`. It has `'wavy'`.
The style field `align` is `'left'`, `'center'`, `'right'`, `'fill'` or `'justify'`. It has `'middle'`.
same style