Skip to content

Novis\Spreadsheet\Sheet::addRow

$sheet->addRow(array<mixed> $row, RowOptions $options = null): Sheet$sheet->addRow(
    array<mixed> $row,
    RowOptions $options = null,
): Sheet

Adds one row to the end of the sheet and returns the sheet, so the next call can follow it.

When the row has keys, each value goes to the column of its key, and the keys of the first row are the header row. When the row has no keys, the values go to the columns in order, from column A. A value can be text, a number, a date, a Formula or a Cell.

$options changes how the row looks. height is the row height in points. hidden: true hides the row. level puts the row in an outline group. style is a Style for every cell of the row.

Good to know: the rows are written in order, and a row cannot be changed after you add it. A sheet is finished when the workbook starts the next sheet. After that, addRow throws a LogicError. A CSV file has no heights, styles or hidden rows, so these options do nothing there. A number that is NaN or an infinity throws a LogicError that names its cell, in every format.

The examples below add rows with keys, add rows without keys, and hide a row.

Parameters

  • $rowarray<mixed>
  • $optionsRowOptionsdefault null

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

Rows with keys

<?nvs
// Shows how to add rows with keys to a sheet.
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory({ format: 'csv' });
var $sheet = $book->addSheet('Orders');
// Each value goes to the column of its key. The keys of the first row are the header row.
$sheet->addRow(['order' => 1042, 'customer' => 'Ann', 'total' => 19.9]);
$sheet->addRow(['order' => 1043, 'customer' => 'Ben', 'total' => 5.5]);
var $written = $book->finish();
if ($written->bytes != null) {
echo $written->bytes as string;
}
Output
order,customer,total␍
1042,Ann,19.9␍
1043,Ben,5.5␍

Rows without keys

<?nvs
// Shows how to add rows without keys, one call after another.
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory({ format: 'csv' });
// These rows have no keys, so the values go to the columns in order, from column `A`.
// `addRow` returns the sheet, so the next `addRow` can follow it.
$book->addSheet('Stock')->addRow(['Item', 'In stock'])->addRow(['Pen', 120])->addRow(['Ink', 35]);
var $written = $book->finish();
if ($written->bytes != null) {
echo $written->bytes as string;
}
Output
Item,In stock␍
Pen,120␍
Ink,35␍

Hide a row and make a row taller

<?nvs
// Shows how to hide a row, and how to make a row taller and bold.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Style;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Prices');
// `height` is in points. `style` applies to every cell of the row.
$sheet->addRow(['item' => 'Pen', 'price' => 1.5], { height: 30.0, style: Style::of({ bold: true }) });
// `hidden: true` hides this row in Excel. The row is still in the file.
$sheet->addRow(['item' => 'Old pen', 'price' => 0.9], { hidden: true });
$sheet->addRow(['item' => 'Ink', 'price' => 2.0]);
bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// `hidden: 'skip'` reads only the rows a person sees. This prints rows 2 and 4.
foreach (Reader::open($file)->rows('Prices', { header: true, hidden: 'skip' }) as var $row) {
echo $row->number, ': ', $row->values['item'], "\n";
}
Output
2: Pen
4: Ink