Novis\Spreadsheet\Reader::sheets
Returns the sheets of the workbook, in the order of their tabs in Excel.
Each sheet has its name, its position from 0, and its visibility. visibility is Visibility::Visible, Visibility::Hidden or Visibility::VeryHidden. A very hidden sheet is one that a person cannot show again from the Excel menus. kind says whether the sheet is a worksheet or a chart sheet. rows and columns are the size that the file states for the sheet. When the file states no size, sheets counts the rows and columns of the sheet. rows is then the number of the last row with a cell. A damaged file can list a sheet that it does not contain. That sheet has null for rows and columns, and reading its rows throws a ParseError.
Good to know: the name of a sheet comes from the file, so it is tainted. You can pass the name or the position to rows, merged, links, notes, tables or images. The reader reads the sizes once and keeps them, so a second call to sheets is fast. A CSV file has one sheet, Sheet1.
The examples below list the sheets, find the hidden ones, and read every sheet.
Return value
Returns array<SheetInfo>.
Examples
List the sheets
<?nvs// Shows how to list the sheets of a workbook by name and position.use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();$book->addSheet('Orders')->addRow(['Order', 'Total']);$book->addSheet('Customers')->addRow(['Name', 'City']);$book->addSheet('Notes')->addRow(['Text']);var $reader = Reader::open($book->finish()->bytes ?? Core\Bytes::fill(0, 0));
// The sheets are in tab order. The first sheet is at position 0.foreach ($reader->sheets() as var $info) { echo $info->position, ': ', $info->name, "\n";}0: Orders1: Customers2: NotesSkip hidden sheets
<?nvs// Shows how to find the sheets a person can see, and skip the hidden ones.use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();$book->addSheet('Prices')->addRow(['Item', 'Price']);$book->addSheet('Lookup', { hidden: true })->addRow(['Code', 'Rate']);$book->addSheet('Summary')->addRow(['Total']);var $reader = Reader::open($book->finish()->bytes ?? Core\Bytes::fill(0, 0));
// `visibility` is `Reader::Visibility::Visible` for a sheet that shows a tab in Excel.foreach ($reader->sheets() as var $info) { if ($info->visibility == Reader::Visibility::Visible) { echo $info->name, " is visible\n"; } else { echo $info->name, " is hidden\n"; }}Prices is visibleLookup is hiddenSummary is visibleRead every sheet
<?nvs// Shows how to read every sheet of a workbook, one after another.use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();$book->addSheet('North')->addRows([['Shop', 'Sales'], ['North 1', 120], ['North 2', 80]]);$book->addSheet('South')->addRows([['Shop', 'Sales'], ['South 1', 200]]);var $reader = Reader::open($book->finish()->bytes ?? Core\Bytes::fill(0, 0));
// `rows` takes the position of the sheet, so the loop does not need to know the names.foreach ($reader->sheets() as var $info) { int $total = 0; foreach ($reader->rows($info->position as int, { header: true }) as var $row) { $total += $row->values['Sales'] as int; } echo $info->name, ' sold ', $total, "\n";}North sold 200South sold 200