Novis\Spreadsheet\Sheet::addTable
$sheet->addTable(string $range, TableOptions $options = null): Sheet$sheet->addTable( string $range, TableOptions $options = null, ): Sheet
Makes the cells of $range, such as "A1:C9", an Excel table, and returns the sheet. The first row of the range is the header row of the table, and each header needs its own name.
name is the name of the table. style is the name of an Excel table style. banded colors every second row, and filter shows a filter button in each header. Both are true unless you set them to false. totals adds a totals row after the table. It maps a header to "sum", "average", "count", "min", "max" or a Formula. Any other total throws a LogicError.
Good to know: the totals row is the row after the table. You can add the table after its last row. Then the totals row is written at once, and your next row comes after it. A CSV file has no tables, so addTable does nothing there.
The examples below make a table of orders, add a totals row, and show the error for a total that is not allowed.
Parameters
$rangestring$optionsTableOptionsdefault 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
A table of orders
<?nvs// Shows how to make the orders of a sheet an Excel table with a name.use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Orders');$sheet->addRows([ ['order' => 1042, 'customer' => 'Ann', 'total' => 19.9], ['order' => 1043, 'customer' => 'Ben', 'total' => 5.5],]);
// Row 1 is the header row, and the orders are in rows 2 and 3.$sheet->addTable('A1:C3', { name: 'Orders', style: 'TableStyleMedium2' });bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
foreach (Reader::open($file)->tables('Orders') as var $table) { echo $table->name, ' covers ', $table->table, "\n";}Orders covers A1:C3A totals row
<?nvs// Shows how to add a totals row that sums the sales of every shop.use Novis\Spreadsheet\Formula;use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Sales');$sheet->addRows([['shop' => 'North', 'sales' => 120], ['shop' => 'South', 'sales' => 80], ['shop' => 'East', 'sales' => 95]]);
// The table covers rows 1 to 4, so the totals row is row 5.$sheet->addTable('A1:B4', { name: 'Sales', totals: ['sales' => 'sum'] });bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// With `formulas: true`, the reader returns each formula as a `Formula`.// This prints the formula of row 5. `SUBTOTAL` skips the rows a filter hides.foreach (Reader::open($file)->rows('Sales', { header: true, formulas: true }) as var $row) { foreach ($row->values as var $value) { if ($value is Formula) { echo $row->number, ': ', $value->text, "\n"; } }}5: SUBTOTAL(109,Sales[sales])A total that is not allowed
<?nvs// Shows the error for a total that is not one of the five names.use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Sales');$sheet->addRows([['shop' => 'North', 'sales' => 120], ['shop' => 'South', 'sales' => 80]]);
// `median` is not allowed, so `addTable` throws a `LogicError`. A `Formula` can compute it.try { $sheet->addTable('A1:B3', { totals: ['sales' => 'median'] });} catch (LogicError $e) { echo $e->message, "\n";}The total of `sales` is `'sum'`, `'average'`, `'count'`, `'min'`, `'max'` or a `Formula`. It has `'median'`.