Skip to content

Novis\Spreadsheet\Formula::of

Makes an Excel formula for one cell, such as SUM(C2:C9).

Write the formula the way you write it in Excel, with or without = at the start. An empty formula throws a LogicError. A formula has at most 8,192 characters, as in Excel. A longer one throws a LogicError when its row is written. Put the result of Formula::of in a row in place of a value. Only a Formula is written as a formula. A string is always written as text, even when it starts with =. So text that a user typed can never run as a formula in the file.

cached is the result that the file has for the formula. A program that reads the file and does not calculate formulas gets this value. Excel calculates every formula again when it opens the file.

Good to know: $sheet->cell and $sheet->column give the address of a cell and of a column, so your program never needs to work out a column letter.

Parameters

  • $textstring
  • $options?{ cached?: string|int|float }default null

Return value

Returns Formula.

Examples

A totals row with a sum

<?nvs
// Shows how to add a totals row whose cell is a formula, with the result already in the file.
use Novis\Spreadsheet\Formula;
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Orders');
$sheet->addRows([['item' => 'Pen', 'total' => 3.0], ['item' => 'Ink', 'total' => 4.25]]);
// `column("total")` is the range of the rows written so far, here `B2:B3`.
// `cached` is the result for a program that reads the file and does not calculate.
$sheet->addRow(['item' => 'Total', 'total' => Formula::of('SUM(' . $sheet->column('total') . ')', { cached: 7.25 })]);
bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// `Reader` gives the cached result of a formula cell.
foreach (Reader::open($file)->rows('Orders', { header: true }) as var $row) {
echo $row->values['item'], ': ', $row->values['total'], "\n";
}
Output
Pen: 3
Ink: 4.25
Total: 7.25

Read a formula back

<?nvs
// Shows how to read the formula of a cell back, and not only its result.
use Novis\Spreadsheet\Formula;
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
// The `=` at the start is allowed. `Formula::of` removes it.
bytes $file = Workbook::fromRows([['price' => 2.5, 'count' => 4, 'total' => Formula::of('=A2*B2', { cached: 10 })]])->bytes();
// With `formulas: true`, a formula cell is read as a `Formula`.
foreach (Reader::open($file)->rows('Sheet1', { header: true, formulas: true }) as var $row) {
mixed $total = $row->values['total'];
if ($total is Formula) {
// `text` is tainted, because a file can come from a user. This program wrote the file itself.
echo 'formula: ', Core\Taint::assertTrusted($total->text, 'this program wrote the file'), "\n";
echo 'cached: ', $total->cached, "\n";
}
}
Output
formula: A2*B2
cached: 10

Text from a user stays text

<?nvs
// Shows that a string that starts with `=` is written as text, and only a `Formula` is a formula.
use Novis\Spreadsheet\Formula;
use Novis\Spreadsheet\Workbook;
// A user typed this comment. In a spreadsheet, it would start a formula.
string $comment = '=1+1';
// In CSV, the text gets a `'` in front, so a spreadsheet program shows it as text.
echo Workbook::fromRows([
['kind' => 'comment', 'value' => $comment],
['kind' => 'formula', 'value' => Formula::of('1+1')],
], { format: 'csv' })->bytes() as string;
Output
kind,value␍
comment,'=1+1␍
formula,=1+1␍