Novis\Spreadsheet\Reader::rows
Reads the rows of one sheet, one Row at a time.
$sheet is the sheet name, or its position from 0. Each Row has number, the row number in the file, and values. The keys of values are the column letters, such as A. With header: true, the first row that is not empty gives the keys instead. header: 2 takes them from row 2. from is the first row to read, limit is the most rows, and columns reads only some columns.
Good to know: empty rows are skipped. Use emptyRows: 'keep' to get them. A row of formulas with no saved result is not empty, and each of its values is null. With merged: 'fill', each cell of a merged range has the value of its top-left cell. A string from the file is tainted, so check it before you trust it. The rows are read in batches, so a large sheet is never all in memory.
Parameters
$sheettainted string|int$optionsRowOptionsdefault null
Return value
Returns Iterator<Row>.
Examples
An import with errors by row number
<?nvs// Shows how to import an uploaded price list and report each bad row by its row number.use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Workbook;
// This makes the upload. In a real program the bytes come from the request.var $book = Workbook::createInMemory();$book->addSheet('Prices')->addRows([ ['Item', 'Price'], ['Widget', 2.5], ['Gadget', 'free'], [null, 4.0], ['Gizmo', 1.25],]);bytes $upload = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// `header: true` uses row 1 for the keys. `$row->number` is the row number a person sees in Excel.array<string> $errors = [];int $imported = 0;foreach (Reader::open($upload)->rows('Prices', { header: true }) as var $row) { mixed $price = $row->values['Price']; if ($row->values['Item'] == null) { $errors[] = "Row {$row->number}: the item name is missing."; } elseif (!($price is int || $price is float)) { $errors[] = "Row {$row->number}: the price is not a number."; } else { $imported++; }}
echo $imported, " rows imported\n";foreach ($errors as string $error) { echo $error, "\n";}2 rows importedRow 3: the price is not a number.Row 4: the item name is missing.A zip code column read as text
<?nvs// Shows how to read a zip code column as text, so a zip code keeps its leading zero.use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Workbook;
// In this file a zip code is a number with the format `00000`. Excel shows 2134 as `02134`.bytes $file = Workbook::fromRows( [['shop' => 'North', 'zip' => 2134], ['shop' => 'South', 'zip' => 98101]], { columns: ['shop' => { header: 'Shop' }, 'zip' => { header: 'Zip', format: '00000' }] },)->bytes();var $reader = Reader::open($file);
// Without `text`, the cell is the number 2134.foreach ($reader->rows(0, { header: true }) as var $row) { echo $row->values['Shop'], ': ', $row->values['Zip'], "\n";}
// `text: ['Zip']` reads that column as the text Excel shows. The result is the string `02134`.foreach ($reader->rows(0, { header: true, text: ['Zip'] }) as var $row) { echo $row->values['Shop'], ': ', $row->values['Zip'], "\n";}North: 2134South: 98101North: 02134South: 98101A page of rows and some columns
<?nvs// Shows how to read only some rows and some columns of a large sheet.use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Orders');$sheet->addRow(['Order', 'Customer', 'Note', 'Total']);for (int $n = 1; $n <= 50; $n++) { $sheet->addRow([1000 + $n, "Customer {$n}", 'none', $n * 10]);}var $reader = Reader::open($book->finish()->bytes ?? Core\Bytes::fill(0, 0));
// `header: 1` says that row 1 has the keys. `from: 12` starts at row 12, and `limit: 3` reads three rows.// `columns` reads only the columns A and D.foreach ($reader->rows('Orders', { header: 1, from: 12, limit: 3, columns: ['A', 'D'] }) as var $row) { echo 'row ', $row->number, ': order ', $row->values['Order'], ', total ', $row->values['Total'], "\n";}
// Without a header, the keys are the column letters.foreach ($reader->rows('Orders', { limit: 1 }) as var $row) { echo Core\Str::join(Core\Arr::keys($row->values), ', '), "\n";}row 12: order 1011, total 110row 13: order 1012, total 120row 14: order 1013, total 130A, B, C, D