Skip to content

Novis\Spreadsheet\Template::set

$template->set(string $address, mixed $value): Template$template->set(
    string $address,
    mixed $value,
): Template

Writes one value into one cell of a template, by the cell's address.

The address is the sheet name, then !, then the cell, such as 'Summary!B2'. The cell does not need a placeholder. set writes the value with the same types as fill: text, a number, a boolean, a date or time from Core\Time, null or a Formula. A string that starts with = is written as text. Use Formula::of to write a formula. set returns the template, so you can call it several times in a row and then call fill.

Good to know: call set before fill. An address that names a sheet the template does not have throws a LogicError.

The examples below write a title into a cell, fill a summary sheet beside an item list, and write a value of each type.

Parameters

  • $addressstring
  • $valuemixed

Return value

Returns Template.

Examples

A title in a cell by address

<?nvs
// Shows how to write a value into one cell of a template by its address.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Template;
use Novis\Spreadsheet\Workbook;
var $design = Workbook::createInMemory();
$design->addSheet('Report')->addRows([['Report', null], ['Sales', '{{sales}}']]);
bytes $template = $design->finish()->bytes ?? Core\Bytes::fill(0, 0);
// Cell B1 has no placeholder. `set` writes the value there. Call `set` before `fill`.
bytes $file = Template::open($template)->set('Report!B1', 'May 2024')->fill(['sales' => 1200])->bytes();
foreach (Reader::open($file)->rows('Report') as var $row) {
echo $row->values['A'], ': ', $row->values['B'], "\n";
}
Output
Report: May 2024
Sales: 1200

A summary sheet and an item sheet

<?nvs
// Shows how to fill a summary sheet with `set` and an item sheet with `fill`.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Template;
use Novis\Spreadsheet\Workbook;
var $design = Workbook::createInMemory();
$design->addSheet('Summary')->addRows([['Orders', null], ['Customer', null]]);
$design->addSheet('Orders')->addRows([['Order', 'Total'], ['{{orders.id}}', '{{orders.total}}']]);
bytes $source = $design->finish()->bytes ?? Core\Bytes::fill(0, 0);
// The address names the sheet, then `!`, then the cell.
var $template = Template::open($source);
$template->set('Summary!B1', 2);
$template->set('Summary!B2', 'Shop');
bytes $file = $template->fill(['orders' => [['id' => 1042, 'total' => 19.5], ['id' => 1043, 'total' => 5.0]]])->bytes();
var $reader = Reader::open($file);
array<string> $sheets = ['Summary', 'Orders'];
foreach ($sheets as string $sheet) {
foreach ($reader->rows($sheet) as var $row) {
echo $sheet, ' row ', $row->number, ': ', $row->values['A'], ', ', $row->values['B'], "\n";
}
}
Output
Summary row 1: Orders, 2
Summary row 2: Customer, Shop
Orders row 1: Order, Total
Orders row 2: 1042, 19.5
Orders row 3: 1043, 5

Values of each type

<?nvs
// Shows the types of value that `set` can write, and what happens with a wrong address.
use Novis\Spreadsheet\Formula;
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Template;
use Novis\Spreadsheet\Workbook;
var $design = Workbook::createInMemory();
$design->addSheet('Values')->addRows([['Text'], ['Number'], ['Boolean'], ['Formula'], ['Text with =']]);
bytes $source = $design->finish()->bytes ?? Core\Bytes::fill(0, 0);
// A string that starts with `=` is written as text. Use `Formula::of` to write a formula.
var $template = Template::open($source)
->set('Values!B1', 'Shop')
->set('Values!B2', 19.5)
->set('Values!B3', true)
->set('Values!B4', Formula::of('B2*2'))
->set('Values!B5', '=B2*2');
// An address must name a sheet of the template. This one does not, so `set` throws an error.
try {
$template->set('Prices!A1', 1);
} catch (Throwable $e) {
echo $e::class, ': ', $e->message, "\n";
}
bytes $file = $template->fill([])->bytes();
foreach (Reader::open($file)->rows('Values', { formulas: true }) as var $row) {
mixed $value = $row->values['B'];
echo $row->values['A'], ': ', $value is Formula ? 'the formula ' . $value->text : Core\Json::encode($value), "\n";
}
Output
LogicError: The template has no sheet `Prices`, which `Prices!A1` names.
Text: "Shop"
Number: 19.5
Boolean: true
Formula: the formula B2*2
Text with =: "=B2*2"