Skip to content

Novis\Spreadsheet\Workbook::defineName

$workbook->defineName(string $name, string $reference): Workbook$workbook->defineName(
    string $name,
    string $reference,
): Workbook

Gives a cell or a range of cells a name, so a formula can use the name instead of the address.

$name starts with a letter or _, and has only letters, digits, _ and .. It has at most 255 characters. It cannot look like a cell, such as C3 or R1C1. Each name can be used once.

$reference is a sheet name, ! and a cell or a range, such as Orders!$B$2:$B$9. It can also be whole columns or rows, such as Orders!$A:$C or Orders!$1:$3. Put a sheet name with a space or another sign in single quotes: 'Shop orders'!$A$1. Another file, a web address and a formula are not allowed.

A name or a reference that breaks these rules throws a LogicError. If its sheet is not in the workbook, finish() throws a LogicError.

Good to know: you can define a name before or after its sheet, and the whole workbook can use it.

Parameters

  • $namestring
  • $referencestring

Return value

Returns Workbook.

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 name for the order totals

<?nvs
use Novis\Spreadsheet\Formula;
use Novis\Spreadsheet\Reader;
// Shows how to give a range of cells a name, and use the name in a formula.
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $orders = $book->addSheet('Orders');
$orders->addRows([
['order' => 1042, 'total' => 19.9],
['order' => 1043, 'total' => 5.0],
['order' => 1044, 'total' => 12.5],
]);
// The totals are in the cells `B2` to `B4`. The `$` signs make each address fixed.
$book->defineName('Totals', "Orders!\$B\$2:\$B\$4");
// A formula in another sheet can now use the name `Totals`.
$book->addSheet('Summary')->addRow(['sum' => Formula::of('SUM(Totals)')]);
var $written = $book->finish();
if ($written->bytes != null) {
// A name with no sheet is one the whole workbook can use. The file also has a name for each
// filter, and each of those names belongs to one sheet.
foreach (Reader::open($written->bytes)->names() as var $name) {
if ($name->sheet == null) {
echo $name->name, ' = ', $name->reference, "\n";
}
}
}
Output
Totals = Orders!$B$2:$B$4

Names that excel does not allow

<?nvs
// Shows the names that `defineName` does not allow.
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
$book->addSheet('Prices')->addRow(['Pen', 1.5]);
// `C3` is a cell, `1st` starts with a digit, and `Price list` has a space. `Prices.pen` is allowed.
// Excel ignores capital letters, so `PRICES.PEN` repeats `Prices.pen` and throws a `LogicError`.
array<string> $names = ['C3', '1st', 'Price list', 'Prices.pen', 'PRICES.PEN'];
foreach ($names as string $name) {
try {
$book->defineName($name, "Prices!\$B\$1");
echo $name, ": defined\n";
} catch (LogicError $e) {
echo $name, ': ', $e->message, "\n";
}
}
$book->finish();
Output
C3: `C3` is a cell reference, so it cannot be a name.
1st: `1st` is not a name Excel allows. A name starts with a letter or `_`, and has only letters, digits, `_` and `.`.
Price list: `Price list` is not a name Excel allows. A name starts with a letter or `_`, and has only letters, digits, `_` and `.`.
Prices.pen: defined
PRICES.PEN: The name `PRICES.PEN` is defined twice.

Tax rates for an invoice

<?nvs
use Novis\Spreadsheet\Formula;
use Novis\Spreadsheet\Reader;
// Shows an invoice whose formulas use named tax rates instead of cell addresses.
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
$book->addSheet('Rates')->addRows([['rate' => 'standard', 'value' => 0.2], ['rate' => 'reduced', 'value' => 0.05]]);
// `defineName` returns the workbook, so you can define several names in a row.
$book->defineName('Standard', "Rates!\$B\$2")->defineName('Reduced', "Rates!\$B\$3");
// `cached` is the result that a program sees when it reads the file. Excel calculates the formula again.
var $invoice = $book->addSheet('Invoice');
$invoice->addRow(['item' => 'Book', 'price' => 20.0, 'tax' => Formula::of('B2*Reduced', { cached: 1.0 })]);
$invoice->addRow(['item' => 'Lamp', 'price' => 40.0, 'tax' => Formula::of('B3*Standard', { cached: 8.0 })]);
var $written = $book->finish();
if ($written->bytes != null) {
foreach (Reader::open($written->bytes)->rows('Invoice', { header: true }) as var $row) {
echo $row->values['item'], ' tax: ', $row->values['tax'], "\n";
}
}
Output
Book tax: 1
Lamp tax: 8