Novis\Spreadsheet\Workbook::finish
$workbook->finish(): WrittenWrites the end of the spreadsheet and returns a Written with what was written.
Call finish once, after the last row. It finishes the last sheet and writes the end of the file. Then it sends that last part to the browser, the file or memory. A file from createForFile is closed. A file from createForStream stays open.
The Written it returns has rows, the number of rows of each sheet by sheet name, and size, the length of the file in bytes. The header row is not counted. bytes is the whole file for createInMemory, and null for the other targets.
Good to know: a workbook that is never finished is not a valid file, and the program logs an error for it. A workbook needs at least one sheet, and each call after finish throws a LogicError.
Return value
Returns Written.
Examples
The rows of each sheet
<?nvs// Shows what `finish` returns: the rows of each sheet and the size of the file.use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();$book->addSheet('Orders')->addRows([['order' => 1042], ['order' => 1043], ['order' => 1044]]);$book->addSheet('Returns')->addRow(['order' => 1040]);
// `rows` has the number of rows of each sheet, by sheet name. The header row is not counted.var $written = $book->finish();foreach ($written->rows as string $sheet => int $count) { echo $sheet, ': ', $count, "\n";}
// `size` is the length of the file in bytes.echo $written->size == Core\Bytes::length($written->bytes ?? Core\Bytes::fill(0, 0)) ? 'the size matches the file' : 'the size is wrong', "\n";Orders: 3Returns: 1the size matches the fileFinish is the last call
<?nvs// Shows that a workbook cannot change after `finish`.use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $orders = $book->addSheet('Orders');$orders->addRow(['order' => 1042, 'total' => 19.9]);$book->finish();
// Each call after `finish` throws a `LogicError`.try { $orders->addRow(['order' => 1043, 'total' => 5.0]);} catch (LogicError $e) { echo $e->message, "\n";}try { $book->finish();} catch (LogicError $e) { echo $e->message, "\n";}The sheet `Orders` is finished, because the workbook is finished. `finish` is the last call on a workbook.The workbook is finished. `finish` is the last call on a workbook.A workbook needs a sheet
<?nvs// Shows an export that finds no rows, and still writes a valid file.use Novis\Spreadsheet\Workbook;
array<array<mixed>> $orders = [];
// A workbook needs at least one sheet. Without one, `finish` throws a `LogicError`.var $empty = Workbook::createInMemory();try { $empty->finish();} catch (LogicError $e) { echo $e->message, "\n";}
// Add the sheet even when there are no rows. The file then has one empty sheet.var $book = Workbook::createInMemory();$book->addSheet('Orders')->addRows($orders);var $written = $book->finish();echo $written->rows['Orders'], " rows\n";if ($written->bytes != null) { foreach (Novis\Spreadsheet\Reader::open($written->bytes)->sheets() as var $sheet) { echo 'the file has the sheet ', $sheet->name, "\n"; }}A workbook needs at least one sheet. Call `addSheet()` before `finish()`.0 rowsthe file has the sheet Orders