Novis\Spreadsheet\Template::open
Novis\Spreadsheet\Template::open(
bytes|Core\IO\File $source,
Options $options = null,
): TemplateOpens a template: an Excel workbook (xlsx or xlsm) with placeholders such as {{customer.name}} in its cells.
$source is the bytes of the file, or a Core\IO\File that your program opened. Template::open checks the file and returns a Template. Then you call set and fill, and choose where the file goes with save, to, bytes or download. Open the template again for each file you make.
Good to know: the filled file copies the formulas and links of the template without a change. For this reason, $source cannot be tainted, such as an uploaded file. A file that is not an Excel workbook throws an error. The option zone, such as 'Europe/Berlin', is the time zone for an Instant value. An xlsm template gives an xlsx file, because the macros are not copied.
The examples below open a template file, fill the same template for each customer, and write an Instant with a time zone.
Parameters
$sourcebytes|Core\IO\File$optionsOptionsdefault null
Return value
Returns Template.
Examples
Open a template file
<?nvs// Shows how to open a template file, fill it and save the result as a new file.use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Template;use Novis\Spreadsheet\Workbook;
string $folder = Core\IO::createTemporaryDirectory();string $templatePath = Core\Path::join($folder, 'order-template.xlsx');string $orderPath = Core\Path::join($folder, 'order-1042.xlsx');
// This saves the template file. In a real program it is a file made in Excel.var $design = Workbook::createForFile($templatePath);$design->addSheet('Order')->addRows([['Order', '{{order}}'], ['Customer', '{{customer}}']]);$design->finish();
// `Template::open` reads the open file. `save` writes the filled order to a new file.Core\IO\File $file = Core\IO::open($templatePath, Core\IO::FileMode::Read);Template::open($file)->fill(['order' => 1042, 'customer' => 'Shop'])->save($orderPath);$file->close();
foreach (Reader::open(Core\IO::open($orderPath, Core\IO::FileMode::Read))->rows('Order') as var $row) { echo $row->values['A'], ': ', $row->values['B'], "\n";}Order: 1042Customer: ShopThe same template for each customer
<?nvs// Shows how to fill one template many times, once for each customer.use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Template;use Novis\Spreadsheet\Workbook;
var $design = Workbook::createInMemory();$design->addSheet('Letter')->addRows([['Dear {{name}}', '{{balance}}']]);bytes $template = $design->finish()->bytes ?? Core\Bytes::fill(0, 0);
// You can call `fill` once on each template. So this loop opens the template bytes again for each customer.array<array<mixed>> $customers = [['name' => 'Shop', 'balance' => 120.5], ['name' => 'Blog', 'balance' => 0.0]];foreach ($customers as array<mixed> $customer) { bytes $file = Template::open($template)->fill($customer)->bytes(); foreach (Reader::open($file)->rows('Letter') as var $row) { echo $row->values['A'], ' has a balance of ', $row->values['B'], "\n"; }}Dear Shop has a balance of 120.5Dear Blog has a balance of 0A time zone for instants
<?nvs// Shows how the `zone` option writes an `Instant` as a date and time in one time zone.use Novis\Spreadsheet\Cell;use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Style;use Novis\Spreadsheet\Template;use Novis\Spreadsheet\Workbook;
var $design = Workbook::createInMemory();$design->addSheet('Log')->addRows([['Shipped', Cell::of('{{shipped}}', Style::of({ format: 'yyyy-mm-dd hh:mm' }))]]);bytes $template = $design->finish()->bytes ?? Core\Bytes::fill(0, 0);
// An `Instant` is one moment, with no time zone. 12:00 in UTC is 14:00 in Berlin in summer.var $shipped = Core\Time\DateTime::parse('2024-06-01 12:00', 'yyyy-MM-dd HH:mm', 'UTC')->toInstant();
// With `zone`, the cell has the local date and time of Berlin.bytes $file = Template::open($template, { zone: 'Europe/Berlin' })->fill(['shipped' => $shipped])->bytes();foreach (Reader::open($file)->rows('Log') as var $row) { mixed $value = $row->values['B']; if ($value is Core\Time\DateTime) { echo 'Shipped: ', $value->format('yyyy-MM-dd HH:mm'), "\n"; }}
// Without `zone`, an `Instant` throws an error, because the time in the cell would be a guess.try { Template::open($template)->fill(['shipped' => $shipped])->bytes();} catch (LogicError $e) { echo $e->message, "\n";}Shipped: 2024-06-01 14:00The value for `{{shipped}}` is an `Instant`, and the template has no `zone`. Set `zone` in the options of `Template::open`, such as `zone: 'Europe/Berlin'`.