Skip to content

Novis\Spreadsheet\Reader::tables

$reader->tables(tainted string|int $sheet): array<Table>$reader->tables(
    tainted string|int $sheet,
): array<Table>

Returns the Excel tables of one sheet. An Excel table is a range of cells that a person made into a table in Excel, with a name and a header row.

$sheet is the sheet name, or its position from 0. Each table has name, the table name such as "Prices", table, its range such as "A1:C10", and sheet, the name of the sheet it is on. The first row of the range is the header row of the table.

Good to know: tables does not read the cells. To read the rows of a table, pass its sheet to rows with the header row as header and the count of rows as limit. Every value comes from the file, so it is tainted. A CSV file has no tables, so the list is empty. A sheet that does not exist throws a LogicError. tables reads xlsx, xlsm, xlsb, xls and ods files. In an ods file, a table is a database range. An xls file has no tables, so the list is empty.

The examples below list the tables of a sheet, read the rows of one table, and find the tables of every sheet.

Parameters

  • $sheettainted string|int

Return value

Returns array<Table>.

Examples

List the tables of a sheet

<?nvs
// Shows how to list the Excel tables of a sheet, with the name and range of each table.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Shop');
$sheet->addTable('A1:B3', { name: 'Prices' });
$sheet->addRows([['Item', 'Price'], ['Widget', 2.5], ['Gadget', 4.0]]);
var $reader = Reader::open($book->finish()->bytes ?? Core\Bytes::fill(0, 0));
// `name` is the table name, `table` is its range, and `sheet` is the sheet it is on.
foreach ($reader->tables('Shop') as var $table) {
echo $table->name, ' is ', $table->sheet, '!', $table->table, "\n";
}
Output
Prices is Shop!A1:B3

Read the rows of a table

<?nvs
// Shows how to find a table by its name and read only its rows.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
// The table `Orders` starts in row 3, below a title.
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Report');
$sheet->addTable('A3:B5', { name: 'Orders' });
$sheet->addRows([['Monthly report'], [null], ['Order', 'Total'], [1001, 120], [1002, 80]]);
var $reader = Reader::open($book->finish()->bytes ?? Core\Bytes::fill(0, 0));
// The range `A3:B5` starts at row 3 and has 3 rows: the header and 2 orders.
foreach ($reader->tables('Report') as var $table) {
if ($table->name == 'Orders') {
echo $table->name, ' is at ', $table->table, "\n";
foreach ($reader->rows('Report', { header: 3, limit: 2 }) as var $row) {
echo 'order ', $row->values['Order'], ': ', $row->values['Total'], "\n";
}
}
}
Output
Orders is at A3:B5
order 1001: 120
order 1002: 80

The tables of every sheet

<?nvs
// Shows how to find every table in a workbook, on all of its sheets.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
$book->addSheet('Shop')->addTable('A1:B2', { name: 'Prices' })->addRows([['Item', 'Price'], ['Widget', 2.5]]);
$book->addSheet('Notes')->addRow(['This sheet has no table.']);
$book->addSheet('Blog')->addTable('A1:A2', { name: 'Posts' })->addRows([['Title'], ['Hello']]);
var $reader = Reader::open($book->finish()->bytes ?? Core\Bytes::fill(0, 0));
// A sheet without tables gives an empty list.
foreach ($reader->sheets() as var $info) {
uint $count = Core\Arr::count($reader->tables($info->position as int));
echo $info->name, ': ', $count, "\n";
}
Output
Shop: 1
Notes: 0
Blog: 1