Skip to content

Novis\Spreadsheet\Sheet::addNote

$sheet->addNote(
    string $cell,
    string $text,
    ?{ author?: string } $options = null,
): Sheet

Adds the note $text to the cell $cell, and returns the sheet.

Excel shows a small red corner on a cell with a note, and shows the note when the mouse is over the cell. author is the name the note shows. Without it, the note has no author.

Good to know: the text of a note is written as plain text. Markup and formulas in it are not run, so a note can show text a user typed. The cell can be in a row you already added or in a row you add later. A CSV file has no notes, so addNote does nothing there.

The examples below add a note to a cell, add a note with an author, and add a note to each row that needs to be checked.

Parameters

  • $cellstring
  • $textstring
  • $options?{ author?: string }default 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

A note on a cell

<?nvs
// Shows how to add a note to one cell.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Prices');
$sheet->addRows([['item' => 'Widget', 'price' => 2.5], ['item' => 'Gadget', 'price' => 4.0]]);
$sheet->addNote('B3', 'This price is valid until the end of the month.');
bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
foreach (Reader::open($file)->notes('Prices') as var $note) {
echo $note->noted, ': ', $note->note, "\n";
}
Output
B3: This price is valid until the end of the month.

A note with an author

<?nvs
// Shows how to add a note with the name of its author.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Orders');
// A note can be added before the row of its cell.
$sheet->addNote('C2', 'The customer asked for a refund.', { author: 'Support' });
$sheet->addRow(['order' => 1042, 'customer' => 'Ann', 'total' => 19.9]);
bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
foreach (Reader::open($file)->notes('Orders') as var $note) {
echo $note->noted, ' by ', $note->author ?? 'nobody', ': ', $note->note, "\n";
}
Output
C2 by Support: The customer asked for a refund.

A note on each row to check

<?nvs
// Shows how to add a note to each row with low stock.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
array<array<mixed>> $stock = [['item' => 'Pen', 'qty' => 120], ['item' => 'Ink', 'qty' => 3], ['item' => 'Pad', 'qty' => 2]];
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Stock');
int $row = 1;
foreach ($stock as array<mixed> $line) {
$sheet->addRow($line);
$row = $row + 1;
// `cell` returns the address of the quantity in this row, such as `B3`.
if ($line['qty'] as int < 5) {
$sheet->addNote($sheet->cell('qty', $row), 'Order more of this item.');
}
}
bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
foreach (Reader::open($file)->notes('Stock') as var $note) {
echo $note->noted, ': ', $note->note, "\n";
}
Output
B3: Order more of this item.
B4: Order more of this item.