Skip to content

Novis\Spreadsheet\Reader::names

$reader->names(): array<Name>

Returns the defined names of the workbook. A defined name is a name for a cell, a range or a value, such as TaxRate for Shop!$B$2.

Each name has name, reference and sheet. reference is the text of the name as Excel shows it in the Name Manager, such as "Shop!$A$1:$C$9". sheet is the name of the one sheet that can use the name. It is null for a name that the whole workbook can use. Excel also keeps some names for itself, such as _xlnm._FilterDatabase for the range of a filter.

Good to know: every value comes from the file, so it is tainted. names does not read the cells of a reference. Pass the sheet to rows and choose the rows and columns there. A CSV file has no names, so the list is empty. names reads xlsx, xlsm, xlsb, xls and ods files.

The examples below list the names, find one name, and read a name that belongs to one sheet.

Return value

Returns array<Name>.

Examples

List the defined names

<?nvs
// Shows how to list the defined names of a workbook and the cells each name refers to.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
$book->addSheet('Shop')->addRows([['Item', 'Price'], ['Widget', 2.5], ['Gadget', 4.0]]);
$book->defineName('Prices', 'Shop!$B$2:$B$3')->defineName('Items', 'Shop!$A$2:$A$3');
var $reader = Reader::open($book->finish()->bytes ?? Core\Bytes::fill(0, 0));
// `reference` is the text of the name, as Excel shows it in the Name Manager.
foreach ($reader->names() as var $name) {
echo $name->name, ' = ', $name->reference, "\n";
}
Output
Prices = Shop!$B$2:$B$3
Items = Shop!$A$2:$A$3

Find one name

<?nvs
// Shows how to find one defined name, such as the cell of an input field in a form.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
$book->addSheet('Form')->addRows([['Customer', 'Shop'], ['Order date', '2024-05-01']]);
$book->defineName('CustomerName', 'Form!$B$1');
var $reader = Reader::open($book->finish()->bytes ?? Core\Bytes::fill(0, 0));
// This map has each reference by its name.
array<tainted string> $names = [];
foreach ($reader->names() as var $name) {
$names[$name->name] = $name->reference;
}
echo 'CustomerName is at ', $names['CustomerName'] ?? 'no cell', "\n";
echo 'OrderDate is at ', $names['OrderDate'] ?? 'no cell', "\n";
Output
CustomerName is at Form!$B$1
OrderDate is at no cell

A name of one sheet

<?nvs
// Shows the `sheet` of a name, which is set when only one sheet can use the name.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
// `fromRows` adds a filter to the header row. The file keeps the range of the filter as a name of that sheet.
bytes $file = Workbook::fromRows([['item' => 'Widget'], ['item' => 'Gadget']], { sheet: 'Shop' })->bytes();
// `sheet` is `null` for a name that the whole workbook can use.
foreach (Reader::open($file)->names() as var $name) {
echo $name->name, ' = ', $name->reference, ', sheet: ', $name->sheet ?? 'the whole workbook', "\n";
}
// A CSV file never has names.
echo Core\Arr::count(Reader::open("Item\nWidget\n" as bytes)->names()), " names in a CSV file\n";
Output
_xlnm._FilterDatabase = 'Shop'!$A$1:$A$3, sheet: Shop
0 names in a CSV file