Novis\Spreadsheet\Sheet::column
Returns the range of one column in the rows added so far, such as "C2:C1043", so a formula or a chart can use it.
$key is the key of a column, such as "qty", or an upper-case column letter, such as "C". The header row is not in the range, so the range starts at the first row of data.
Good to know: the range ends at the last row you added before the call. Call column after the last row to get every row. A chart is the one exception. A chart with a series range from column also shows the rows you add after it. Before the first row of data, column throws a LogicError. A sheet is finished when the workbook starts the next sheet. After that, column throws a LogicError.
The examples below find the range of a column, add a total row with SUM, and show how the range grows as rows are added.
Parameters
$keystring
Return value
Returns string.
Examples
The range of a column
<?nvs// Shows how to find the range of a column after the last row.use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Orders');$sheet->addRow(['order' => 1042, 'total' => 19.9]);$sheet->addRow(['order' => 1043, 'total' => 5.5]);$sheet->addRow(['order' => 1044, 'total' => 12.0]);
// Row 1 is the header row, so the data is in rows 2 to 4. This prints `B2:B4`.echo $sheet->column('total'), "\n";B2:B4A total row with sum
<?nvs// Shows how to add a total row that sums a column.use Novis\Spreadsheet\Formula;use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Sales');$sheet->addRows([['shop' => 'North', 'sales' => 120], ['shop' => 'South', 'sales' => 80]]);
// `column` is called after the last row of data, so the sum covers every shop.// `cached` is the value a reader sees before Excel calculates the formula.$sheet->addRow(['shop' => 'Total', 'sales' => Formula::of('=SUM(' . $sheet->column('sales') . ')', { cached: 200 })]);bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// Without `formulas: true`, the reader returns the cached value of a formula.foreach (Reader::open($file)->rows('Sales', { header: true }) as var $row) { echo $row->values['shop'], ': ', $row->values['sales'], "\n";}North: 120South: 80Total: 200The range grows with the rows
<?nvs// Shows that the range ends at the last row added before the call.use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Stock');$sheet->addRow(['item' => 'Pen', 'qty' => 120]);
// One row of data is added. This prints `B2:B2`.echo $sheet->column('qty'), "\n";
$sheet->addRow(['item' => 'Ink', 'qty' => 35]);$sheet->addRow(['item' => 'Pad', 'qty' => 60]);
// Three rows of data are added. This prints `B2:B4`.echo $sheet->column('qty'), "\n";B2:B2B2:B4