Skip to content

Novis\Spreadsheet\Workbook::createInMemory

Starts a spreadsheet that your program keeps in memory, as an Excel file (xlsx) or a CSV file.

Add a sheet with addSheet, add rows to it, and call finish at the end. finish returns a Written. Its bytes is the whole file, and its size is the length of the file in bytes. Its rows has the number of rows of each sheet. format: "csv" makes a CSV file. The other options set the time zone for dates and the document properties, such as title and author.

Good to know: the whole file is in memory until your program is done with it. For a large export, createForDownload or createForFile sends each part of the file as soon as it is ready.

The examples below make an Excel file and read it back, make the same rows as CSV, and compare a report with a saved copy.

Parameters

  • $optionsOptionsdefault null

Return value

Returns Workbook.

Examples

An order list kept in memory

<?nvs
use Novis\Spreadsheet\Reader;
// Shows how to make an Excel file in memory and read it back.
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
$book->addSheet('Orders')->addRows([
['order' => 1042, 'customer' => 'Ada', 'total' => 19.9],
['order' => 1043, 'customer' => 'Alan', 'total' => 5.0],
]);
// `finish` returns a `Written`. Its `bytes` is the whole xlsx file.
var $written = $book->finish();
echo $written->rows['Orders'], " rows\n";
// `Reader::open` reads the file again. The first row has the keys, as a header.
if ($written->bytes != null) {
foreach (Reader::open($written->bytes)->rows('Orders') as var $row) {
echo Core\Json::encode($row->values), "\n";
}
}
Output
2 rows
{"A":"order","B":"customer","C":"total"}
{"A":1042,"B":"Ada","C":19.9}
{"A":1043,"B":"Alan","C":5}

The same orders as csv

<?nvs
// Shows how to make a CSV file in memory instead of an Excel file.
use Novis\Spreadsheet\Workbook;
// `format: "csv"` writes CSV. The calls to add rows are the same as for xlsx.
var $book = Workbook::createInMemory({ format: 'csv' });
$book->addSheet('Orders')->addRows([
['order' => 1042, 'customer' => 'Ada', 'total' => 19.9],
['order' => 1043, 'customer' => 'Alan', 'total' => 5.0],
]);
// A CSV file is text, so this prints the whole file.
var $written = $book->finish();
if ($written->bytes != null) {
echo $written->bytes as string;
}
Output
order,customer,total␍
1042,Ada,19.9␍
1043,Alan,5␍

Check a report against a saved copy

<?nvs
// Shows that the same rows always give the same file, so a test can compare the bytes.
use Novis\Spreadsheet\Workbook;
class Report
{
// This function writes the monthly sales report and returns the file.
public static function sales(array<array<mixed>> $rows): bytes
{
var $book = Workbook::createInMemory({ properties: { title: 'Sales', author: 'Shop' } });
$book->addSheet('Sales')->addRows($rows);
return $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
}
}
array<array<mixed>> $rows = [
['month' => 'January', 'sales' => 1200],
['month' => 'February', 'sales' => 950],
];
// The file has no creation time unless you set one, so two runs give the same bytes.
bytes $saved = Report::sales($rows);
echo Report::sales($rows) == $saved ? 'the report is unchanged' : 'the report changed', "\n";
// One changed number gives a different file.
$rows[1]['sales'] = 951;
echo Report::sales($rows) == $saved ? 'the report is unchanged' : 'the report changed', "\n";
Output
the report is unchanged
the report changed