Novis\Spreadsheet\Sheet::cell
Returns the address of one cell, such as "C7", so a formula can use it.
$key is the key of a column, such as "qty", or a column letter, such as "C". A column letter is upper case, from "A" to "XFD". Any other text is a key, so "c" is a key. $row is the row number, and the first row of a sheet is row 1. The header row is row 1 when the rows have keys.
Good to know: a key is known after the first row with keys, or when the column is set in the columns of Workbook::addSheet. Before that, a key throws a LogicError that names the key. A row number below 1, or a column after the last column of a sheet, throws a LogicError. When the workbook starts the next sheet, this sheet is finished. Then cell throws a LogicError that names the sheet.
The examples below find the address of a cell, use two addresses in a formula, and show the error for row 0.
Parameters
$keystring$rowint
Return value
Returns string.
Examples
The address of a cell
<?nvs// Shows how to find the address of a cell from the key of its column.use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Orders');$sheet->addRow(['order' => 1042, 'customer' => 'Ann', 'qty' => 3]);
// `qty` is the third key, so its column is `C`. This prints `C7`.echo $sheet->cell('qty', 7), "\n";
// A column letter works too. This prints `A2`.echo $sheet->cell('A', 2), "\n";C7A2A formula that uses two cells
<?nvs// Shows how to build a formula from the addresses of two cells.use Novis\Spreadsheet\Formula;use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Invoice');$sheet->addRow(['item' => 'Widget', 'price' => 2.5, 'qty' => 4, 'amount' => null]);
// The formula of row 3 multiplies the price and the quantity of row 3.string $price = $sheet->cell('price', 3);string $qty = $sheet->cell('qty', 3);$sheet->addRow(['item' => 'Gadget', 'price' => 4.0, 'qty' => 2, 'amount' => Formula::of("={$price}*{$qty}")]);bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// With `formulas: true`, the reader returns a formula as a `Formula`. This prints `B3*C3`.foreach (Reader::open($file)->rows('Invoice', { header: true, formulas: true }) as var $row) { var $amount = $row->values['amount']; if ($amount is Formula) { echo $row->number, ': ', $amount->text, "\n"; }}3: B3*C3Row 0 is not a row
<?nvs// Shows the error for a row number below 1.use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Orders');$sheet->addRow(['order' => 1042, 'total' => 19.9]);
// The first row of a sheet is row 1, so row 0 throws a `LogicError`.try { $sheet->cell('total', 0);} catch (LogicError $e) { echo $e->message, "\n";}The row number is 0. The first row of a sheet is 1.