Skip to content

Novis\Spreadsheet\Sheet::addRows

Adds each row of $rows to the end of the sheet, in order, and returns the sheet.

Each row is added the way addRow adds it. A row with keys puts each value in the column of its key. A row without keys puts the values in the columns in order, from column A.

$rows can be an array of rows or a generator. A generator gives one row at a time, so a long export does not need all of its rows in memory at once.

Good to know: addRows gives no options to a row. To set the height, the style or hidden of one row, add that row with addRow. A sheet is finished when the workbook starts the next sheet. After that, addRows throws a LogicError.

The examples below add an array of rows, add the rows of a generator, and add a styled header row before the other rows.

Parameters

  • $rowsarray<array<mixed>>|Iterable<array<mixed>>|Iterator<array<mixed>>

Return value

Returns Sheet.

Changelog

VersionChange
0.0.6Each 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

Add an array of rows

<?nvs
// Shows how to add an array of rows to a sheet with one call.
use Novis\Spreadsheet\Workbook;
array<array<mixed>> $orders = [
['order' => 1042, 'shop' => 'North', 'total' => 19.9],
['order' => 1043, 'shop' => 'South', 'total' => 5.5],
['order' => 1044, 'shop' => 'North', 'total' => 12.0],
];
var $book = Workbook::createInMemory({ format: 'csv' });
$book->addSheet('Orders')->addRows($orders);
var $written = $book->finish();
if ($written->bytes != null) {
echo $written->bytes as string;
}
Output
order,shop,total␍
1042,North,19.9␍
1043,South,5.5␍
1044,North,12␍

Add the rows of a generator

<?nvs
// Shows how to add the rows of a generator, one row at a time.
use Novis\Spreadsheet\Workbook;
class Visits
{
// This generator gives one row for each day. A real program reads the rows from a database.
public static function perDay(): Iterator<array<mixed>>
{
int $day = 1;
while ($day <= 4) {
yield ['day' => $day, 'visits' => 100 + $day * 7];
$day = $day + 1;
}
}
}
var $book = Workbook::createInMemory({ format: 'csv' });
$book->addSheet('Visits')->addRows(Visits::perDay());
// `rows` counts the rows of data of each sheet. The header row is not counted.
var $written = $book->finish();
echo $written->rows['Visits'], " rows\n";
if ($written->bytes != null) {
echo $written->bytes as string;
}
Output
4 rows
day,visits␍
1,107␍
2,114␍
3,121␍
4,128␍

A styled header row and then the rows

<?nvs
// Shows how to add a styled header row with `addRow`, and then the other rows with `addRows`.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Style;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Products');
// `addRow` takes options, so the header row gets its own style.
$sheet->addRow(['Product', 'Price'], { style: Style::of({ bold: true, fill: '#DDEBF7' }) });
$sheet->addRows([['Widget', 2.5], ['Gadget', 4.0]]);
bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// The rows have no keys, so `Reader` gives each value under its column letter, such as `'A'`.
foreach (Reader::open($file)->rows('Products') as var $row) {
echo $row->number, ': ', $row->values['A'], ' ', $row->values['B'], "\n";
}
Output
1: Product Price
2: Widget 2.5
3: Gadget 4