Skip to content

Novis\Spreadsheet\Cell::rich

Novis\Spreadsheet\Cell::rich(
    array<{ text: string, style?: Style }> $runs,
): Cell

Writes one text cell whose parts have different fonts, such as a sentence with one bold word.

Give Cell::rich a list of parts. Each part has a text and may have a style. Put the result in a row in place of the value. Excel shows the parts one after another, in one cell.

A part's style may set font, size, bold, italic, strike, underline and color. Any other field throws a LogicError that names the field and the part. An empty list also throws a LogicError. The cell's fill, border, alignment and format come from its row and its column.

The cell's value is null, and its runs property contains the parts.

Good to know: Reader reads the cell as the text of all parts together. A CSV file has no fonts, so it also contains that text.

Parameters

  • $runsarray<{ text: string, style?: Style }>

Return value

Returns Cell.

Examples

One bold word

<?nvs
// Shows how to write a cell with one bold word in its text.
use Novis\Spreadsheet\Cell;
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Style;
use Novis\Spreadsheet\Workbook;
var $bold = Style::of({ bold: true });
// The cell has three parts. Only the middle part is bold.
var $note = Cell::rich([
{ text: 'Pay ' },
{ text: 'before Friday', style: $bold },
{ text: ', please.' },
]);
bytes $file = Workbook::fromRows([['invoice' => 1042, 'note' => $note]])->bytes();
// The reader gives the text of all parts together.
foreach (Reader::open($file)->rows('Sheet1', { header: true }) as var $row) {
echo $row->values['invoice'], ': ', $row->values['note'], "\n";
}
Output
1042: Pay before Friday, please.

A red number in a filled row

<?nvs
// Shows a rich cell in a row with a fill. The parts set the font, and the row sets the fill.
use Novis\Spreadsheet\Cell;
use Novis\Spreadsheet\Style;
use Novis\Spreadsheet\Workbook;
var $red = Style::of({ color: '#C00000', bold: true });
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Stock', { header: false });
// The whole row has a yellow fill. The number in the first cell is red and bold.
$sheet->addRow([Cell::rich([{ text: 'Only ' }, { text: '3', style: $red }, { text: ' left' }]), 'Pens'], {
style: Style::of({ fill: '#FFF2CC' }),
});
// `runs` contains the parts. `value` is `null` for a rich cell.
var $cell = Cell::rich([{ text: 'a' }, { text: 'b', style: $red }]);
echo Core\Arr::count($cell->runs ?? []), ' parts, value is ', $cell->value == null ? 'null' : 'set', "\n";
echo $book->finish()->rows['Stock'], " row written\n";
Output
2 parts, value is null
1 row written

A part may only set the font

<?nvs
// Shows the error you get when a part of a rich cell has a style with a fill.
use Novis\Spreadsheet\Cell;
use Novis\Spreadsheet\Style;
// `fill` is not a font field, so `Cell::rich` throws a `LogicError`.
try {
Cell::rich([{ text: 'Total' }, { text: ' 42', style: Style::of({ fill: '#FFFF00' }) }]);
} catch (LogicError $e) {
echo $e->message, "\n";
}
// An empty list also throws.
try {
Cell::rich([]);
} catch (LogicError $e) {
echo $e->message, "\n";
}
Output
Part 2 of `Cell::rich` has a style with `fill`. A part can only set font fields: `font`, `size`, `bold`, `italic`, `strike`, `underline` and `color`.
`Cell::rich` needs at least one part, such as `[{ text: 'Total' }]`.