SpreadsheetEvaluator.h header

#include <ew/core/SpreadsheetEvaluator.h>

Namespace ew::core

Functions

QString ew::core::spreadsheet::evaluatedValue(const Spreadsheet &sheet, int row, int column)

Evaluates the cell at (row, column) of sheet to the text it should display.

A cell whose text is not a formula (does not begin with '=') displays that text verbatim. A formula ("=<expression>") is parsed and computed over the sheet's cells. A computed value is either a number or text. It supports:

  • decimal numbers, double-quoted text literals ("hello", with "" for an embedded quote), and the arithmetic operators + - * / with the usual precedence, parentheses, and unary minus;
  • the comparison operators = <> < > <= >= (which yield 1 for true and 0 for false) at the lowest precedence, so IF(...) conditions read naturally; two numbers compare numerically, otherwise operands compare as text;
  • A1-style cell references (resolved recursively) – a single reference reads the cell's number or text – and A1:B2 ranges (empty and text cells in a range are skipped); the column and the row may each be pinned with a '$' ($A$1, $A1, A$1), which reads as the same cell as the unpinned form (a pin governs only which cell a formula points at once it is copied elsewhere);
  • the aggregate functions SUM, PRODUCT, AVERAGE, MIN, MAX, and COUNT over comma-separated arguments and ranges;
  • the math functions ABS, INT, SQRT, POWER(base, exp), MOD(a, b), and ROUND(value[, digits]);
  • the logical functions IF(condition, then, else) (whose branches may be text), AND, OR, NOT; and
  • the text functions CONCAT(...), LEFT(text, n), RIGHT(text, n), MID(text, start, length), LEN(text), UPPER(text), LOWER(text), and TRIM(text). A numeric result is formatted for display and text is returned as-is, or one of the error tokens "#REF!" (bad reference), "#CIRC!" (a reference cycle), "#DIV/0!" (division by zero), or "#ERR!" (a malformed formula, arithmetic on non-numeric text, or a function given the wrong number of arguments) is returned.

std::map< std::pair< int, int >, QString > ew::core::spreadsheet::evaluatedValues(const Spreadsheet &sheet)

Evaluates EVERY populated cell of sheet in one pass, returning (row, column) -> the text that cell should display. A cell not in the result is empty and displays nothing.

Each entry is exactly what evaluatedValue would return for that cell, including the error tokens; this is a performance entry point, not a different semantics. The difference is that one call shares its results across the whole sheet, so a cell that twenty formulas depend on is evaluated once rather than twenty times. evaluatedValue must rebuild the dependency order and re-evaluate the whole closure on every call, which makes displaying a sheet cost time quadratic in the length of its formula chains – an ordinary running-total column of 2,000 rows measured 1.6 SECONDS per full repaint before this existed.

Prefer this wherever more than a handful of cells are wanted at once: painting a grid, exporting, or pushing a sheet back to the codex. A cycle still yields "#CIRC!" for the cells whose dependencies reach it, leaving the rest of the sheet correct.