Novis\Spreadsheet\Cell::of
Novis\Spreadsheet\Cell::of(mixed $value, Style $style): CellNovis\Spreadsheet\Cell::of( mixed $value, Style $style, ): Cell
Gives one value in a row its own style, such as a red total or a highlighted name.
Put the result of Cell::of in a row in place of the value. The value is written the same way as without a style. For each style field, the cell uses its own style first, then the style of its row, then the style of its column. So a cell can change one field, such as fill, and keep the other fields of its row.
Good to know: a CSV file has no styles. There, the cell is written as its value alone.
Parameters
$valuemixed$styleStyle
Return value
Returns Cell.
Examples
A highlighted cell
<?nvs// Shows how to highlight one cell of a row, such as an order that is late.use Novis\Spreadsheet\Cell;use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Style;use Novis\Spreadsheet\Workbook;
var $late = Style::of({ fill: '#FFC7CE', color: '#9C0006' });
// Only the `status` cell of the second order has the style. The value is written as usual.bytes $file = Workbook::fromRows([ ['order' => 1042, 'status' => 'shipped'], ['order' => 1043, 'status' => Cell::of('late', $late)],])->bytes();
foreach (Reader::open($file)->rows('Sheet1', { header: true }) as var $row) { echo $row->values['order'], ': ', $row->values['status'], "\n";}1042: shipped1043: lateA percent cell in a money row
<?nvs// Shows how a cell changes one style field and keeps the other fields of its row.use Novis\Spreadsheet\Cell;use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Style;use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Sales', { header: false });
// The row style shows money and is bold. The third cell has its own `format`, so it shows a percent.// It is still bold, because its own style does not set `bold`.$sheet->addRow(['Pens', 1249.5, Cell::of(0.25, Style::of({ format: 'percent' }))], { style: Style::of({ format: 'money', bold: true }) });bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// `text: true` reads each cell as Excel shows it.foreach (Reader::open($file)->rows('Sales', { text: true }) as var $row) { echo $row->values['A'], ' | ', $row->values['B'], ' | ', $row->values['C'], "\n";}Pens | 1,249.50 | 25%The same rows as csv
<?nvs// Shows that a CSV file keeps the value of a styled cell and leaves out its style.use Novis\Spreadsheet\Cell;use Novis\Spreadsheet\Style;use Novis\Spreadsheet\Workbook;
var $bold = Style::of({ bold: true });array<array<mixed>> $rows = [ ['item' => 'Pen', 'price' => 1.5], ['item' => Cell::of('Total', $bold), 'price' => Cell::of(1.5, $bold)],];
// The same rows can make an Excel file or a CSV file. Only the Excel file has the bold text.echo Workbook::fromRows($rows, { format: 'csv' })->bytes() as string;item,price␍Pen,1.5␍Total,1.5␍