Skip to content

Novis\Spreadsheet\Reader::evaluate

$reader->evaluate(): array<Missed>

Computes every formula in the workbook, and returns the cells it could not compute.

A file saves a result next to each formula, and that result can be missing or wrong. After evaluate, rows gives each formula cell the result that evaluate computed, with the cell's number format.

Each entry in the returned list has cell, such as Sheet1!B4, and kind. kind is 'function' for a function that evaluate does not compute, such as TODAY. It is 'reference' for a reference to another file or a range used as one value. It is 'circular' for a formula that reads its own result, and 'token' for formula text it cannot read. It is 'coercion' for a text such as '$1' or '1/15/2024' that a formula uses as a number. Such a cell keeps the result that is saved in the file.

Good to know: reading a file never computes anything until you call evaluate. It reads no other file and no URL. A CSV file has no formulas, so the list is empty.

The examples below check a report's totals, find the cells a workbook cannot compute, and read a formula with its computed result.

Return value

Returns array<Missed>.

Examples

Compute a report

<?nvs
// Shows that `evaluate` computes formulas that have no saved result.
use Novis\Spreadsheet\Formula;
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Orders');
$sheet->addRow(['item' => 'Tea', 'qty' => 3, 'price' => 2.5]);
$sheet->addRow(['item' => 'Cake', 'qty' => 4, 'price' => 4.0]);
// This formula has no saved result.
$sheet->addRow(['item' => 'Total', 'qty' => Formula::of('=SUM(B2:B3)'), 'price' => Formula::of('=SUMPRODUCT(B2:B3,C2:C3)')]);
Reader $reader = Reader::open($book->finish()->bytes ?? Core\Bytes::fill(0, 0));
// The list is empty, so every formula was computed.
echo Core\Arr::count($reader->evaluate()), " cells not computed\n";
foreach ($reader->rows('Orders', { header: true }) as var $row) {
echo $row->values['item'], ': ', $row->values['qty'], ' items, ', $row->values['price'], "\n";
}
Output
0 cells not computed
Tea: 3 items, 2.5
Cake: 4 items, 4
Total: 7 items, 23.5

Reject what cannot be computed

<?nvs
// Shows how to find the cells of an upload that `evaluate` cannot compute.
use Novis\Spreadsheet\Formula;
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
class Import
{
// This method returns one line for each cell that has no computed result.
public static function check(tainted bytes $upload): array<tainted string>
{
array<tainted string> $lines = [];
foreach (Reader::open($upload)->evaluate() as var $missed) {
$lines[] = "{$missed->cell} is {$missed->kind}: {$missed->text}";
}
return $lines;
}
}
// This makes the upload. In a real program the bytes come from the request.
var $book = Workbook::createInMemory();
$book->addSheet('Plan')->addRow(['day' => Formula::of('=TODAY()', { cached: 45306 }), 'next' => Formula::of('=A2+1')]);
foreach (Import::check($book->finish()->bytes ?? Core\Bytes::fill(0, 0)) as tainted string $line) {
echo $line, "\n";
}
Output
Plan!A2 is function: TODAY

Compare the saved result

<?nvs
// Shows how to compare a formula's saved result with the result that `evaluate` computes.
use Novis\Spreadsheet\Formula;
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
// The saved result of this formula is 999. The correct result is 120.
var $book = Workbook::createInMemory();
$book->addSheet('Bill')->addRow(['net' => 100, 'gross' => Formula::of('=A2*1.2', { cached: 999 })]);
bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// Without `evaluate`, the cell has the saved result.
foreach (Reader::open($file)->rows('Bill', { header: true }) as var $row) {
echo 'saved: ', $row->values['gross'], "\n";
}
// With `evaluate`, the cell has the computed result.
Reader $reader = Reader::open($file);
$reader->evaluate();
foreach ($reader->rows('Bill', { header: true }) as var $row) {
echo 'computed: ', $row->values['gross'], "\n";
}
Output
saved: 999
computed: 120