Skip to content

Novis\Spreadsheet\Reader::notes

$reader->notes(tainted string|int $sheet): array<Note>$reader->notes(
    tainted string|int $sheet,
): array<Note>

Returns the notes of one sheet. A note is the short text that Excel shows when you point at a cell with a red corner.

$sheet is the sheet name, or its position from 0. Each note has noted, the cell of the note, such as "B2", note, the text, and author, the name of the person who wrote it. author is null when the note has no author.

Good to know: the text and the author come from the file, so they are tainted. Excel saves some characters, such as a line break, as a code like _x000D_. notes converts each code back to its character. A CSV file has no notes, so the list is empty. A sheet that does not exist throws a LogicError. notes reads xlsx, xlsm, xlsb, xls and ods files. In an ods file, a note is called a comment.

The examples below list the notes of a sheet, read a note without an author, and show each row of an import together with its notes.

Parameters

  • $sheettainted string|int

Return value

Returns array<Note>.

Examples

List the notes of a sheet

<?nvs
// Shows how to list the notes of a sheet, with the cell, the text and the author of each note.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Prices');
$sheet->addNote('B2', 'Price from the new supplier.', { author: 'Ann' })->addNote('B3', 'Check this price.', { author: 'Ben' });
$sheet->addRows([['Item', 'Price'], ['Widget', 2.5], ['Gadget', 4.0]]);
var $reader = Reader::open($book->finish()->bytes ?? Core\Bytes::fill(0, 0));
// `noted` is the cell of the note, `note` is its text, and `author` is who wrote it.
foreach ($reader->notes('Prices') as var $note) {
echo $note->noted, ' (', $note->author ?? 'no author', '): ', $note->note, "\n";
}
Output
B2 (Ann): Price from the new supplier.
B3 (Ben): Check this price.

A note without an author

<?nvs
// Shows that `author` is `null` when a note has no author.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
$book->addSheet('Blog')->addNote('A1', 'Draft, do not publish.')->addRow(['Hello world']);
var $reader = Reader::open($book->finish()->bytes ?? Core\Bytes::fill(0, 0));
foreach ($reader->notes(0) as var $note) {
echo $note->noted, ': ', $note->note, "\n";
echo 'author: ', $note->author == null ? 'null' : 'set', "\n";
}
Output
A1: Draft, do not publish.
author: null

Notes beside the rows they belong to

<?nvs
// Shows how to show each row of an import together with the notes in that row.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Orders');
$sheet->addNote('C3', 'Paid by bank transfer.');
$sheet->addRows([['Order', 'Customer', 'Total'], [1001, 'Shop', 120], [1002, 'Blog', 80]]);
var $reader = Reader::open($book->finish()->bytes ?? Core\Bytes::fill(0, 0));
// This map has the notes by cell, such as `C3`.
array<tainted string> $notes = [];
foreach ($reader->notes('Orders') as var $note) {
$notes[$note->noted] = $note->note;
}
// A cell name is the column letter and the row number. So the note of the total is at `C` and `$row->number`.
foreach ($reader->rows('Orders', { header: true }) as var $row) {
echo 'order ', $row->values['Order'], ': ', $notes["C{$row->number}"] ?? 'no note', "\n";
}
Output
order 1001: no note
order 1002: Paid by bank transfer.