Novis\Spreadsheet\Workbook::addSheet
$workbook->addSheet(string $name, SheetOptions $options = null): Sheet$workbook->addSheet( string $name, SheetOptions $options = null, ): Sheet
Starts a new sheet in a workbook and returns it, so you can add rows to it.
$name is the name on the tab. It has at most 31 characters, cannot have : \ / ? * [ ] or a control character, and cannot start or end with '. Excel keeps the name History for itself, so it is not allowed. Each sheet needs its own name. A name that breaks these rules throws a LogicError. The sheets are written one after another. When you start a new sheet, the sheet before it is finished. Adding a row to a finished sheet throws a LogicError. position sets where the tab shows, so a summary written last can still be the first tab.
When the rows have keys, the first row shows the keys in bold, with a filter, and stays at the top when a person scrolls. header, headerStyle, freeze and autofilter change this, and columns sets the header text, the width and the format of each column.
Good to know: a CSV file has only one sheet, so a second addSheet call throws a LogicError.
Parameters
$namestring$optionsSheetOptionsdefault null
Return value
Returns Sheet.
Changelog
| Version | Change |
|---|---|
0.0.6 | Each Novis\Spreadsheet\Sheet and Workbook method that adds to the file says what it adds, for example addRow(), addChart() and addSheet(). setPrintOptions() replaces print(). Chart->withSeries() and combinedWith() return a new chart. |
Examples
Two sheets with the summary first
<?nvsuse Novis\Spreadsheet\Reader;// Shows how to add two sheets and show the second one as the first tab.use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
// The orders are written first. The totals are known only after the last order.$book->addSheet('Orders')->addRows([ ['order' => 1042, 'total' => 19.9], ['order' => 1043, 'total' => 5.0],]);
// `position: 0` puts the summary at the first tab. A new sheet finishes the sheet before it.$book->addSheet('Summary', { position: 0 })->addRow(['orders' => 2, 'total' => 24.9]);
var $written = $book->finish();if ($written->bytes != null) { foreach (Reader::open($written->bytes)->sheets() as var $sheet) { echo $sheet->position, ' ', $sheet->name, "\n"; }}0 Summary1 OrdersColumn headers and widths
<?nvs// Shows how to set the header text of each column when you start a sheet.use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory({ format: 'csv' });
// Each entry of `columns` is one column, from column `A`. `header` is the text of its first row.// `width` is the width of the column in characters. A CSV file has no widths, but xlsx does.var $sheet = $book->addSheet('Stock', {columns: [ { header: 'Item', width: 20 }, { header: 'In stock' },]});
// These rows have no keys, so the values go to the columns in order.$sheet->addRow(['Pen', 120]);$sheet->addRow(['Ink', 35]);
var $written = $book->finish();if ($written->bytes != null) { echo $written->bytes as string;}Item,In stock␍Pen,120␍Ink,35␍A csv file has one sheet
<?nvs// Shows what happens when a CSV export tries to add a second sheet.use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory({ format: 'csv' });$book->addSheet('Orders')->addRow(['order' => 1042, 'total' => 19.9]);
// A CSV file has only one sheet, so `addSheet` throws a `LogicError` here.try { $book->addSheet('Returns');} catch (LogicError $e) { echo $e->message, "\n";}
// The first sheet is still complete, and `finish` writes it.var $written = $book->finish();echo $written->rows['Orders'], " row\n";A CSV file has one sheet, so the sheet `Returns` cannot be added. The file already has the sheet `Orders`.1 row