Skip to main content

Evaluating formulas

The evaluator implements Excel’s semantics in Python and runs over a DependencyGraph.

Conceptually

  • FormulaEvaluator is a wrapper around DependencyGraph that:
    • Translates Excel formulas to Python at runtime.
    • Provides Python equivalents for Excel functions, operators, and error types.
    • Handles circular references in a way compatible with Excel’s defaults (warn + return 0, etc.).
    • Caches results to ensure each cell is computed at most once in a given evaluation.

This gives fast, accurate, repeatable execution for any given workbook, but keeps the logic in an Excel-shaped representation. It’s the easiest path when you want to:

  • Re-extract and re-run a computation whenever the workbook changes.
  • Keep a tight coupling to Excel while still running logic in Python.

Range operands

Multi-cell range arguments are classified at resolve time:

  • Lookup consumers (MATCH / VLOOKUP / …) bind lazy Range from excel_grapher.core.grid and evaluate only the cells they touch.
  • Binary / unary operators bind the same lazy Range and map via shared core.operator_maps (aligned with export xl_map_*). Large ops may opt into the ndarray fast path after an explicit materialization buffer when the optional fast extra (NumPy) is installed (results are nested lists at the CellValue boundary).
  • Full-scan reductions (SUM / SUMPRODUCT / COUNTIF / …) also bind lazy Range and walk cells in row-major order via flatten / Grid. SUM and friends fail-fast on the first cell error; COUNTIF / AVERAGEIF skip error cells in the criteria range (Excel). Large SUMPRODUCT may materialize once for the NumPy fast path when excel-grapher[fast] is installed (internal buffer only).
  • AND / OR still reject multi-cell ranges with #VALUE! until cell-wise range support lands.
  • Other functions reject multi-cell ranges with #VALUE! (no object-repr leaks; sibling cells stay unevaluated).

CellValue in the evaluator path is scalar, reference geometry, or nested-list grid results — not persisted np.ndarray operands. NumPy is optional: install excel-grapher[fast] for operator / SUMPRODUCT acceleration on large workbooks; without it the same semantics use shared Grid / nested-list loops.

Scalar coercions (as_scalar) reinforce the same boundary. Lookups and full-scan reductions skip the AST error precheck so each consumer owns its traversal. Errors remain XlError sentinels (the evaluator never raises XlErrorException).

Minimal evaluator example

from excel_grapher.grapher import create_dependency_graph
from excel_grapher.evaluator import FormulaEvaluator

targets = ["Sheet1!B10"]

graph = create_dependency_graph(
    "model.xlsx",
    targets,
    load_values=True,
    max_depth=10,
)

with FormulaEvaluator(graph) as ev:
    evaluator_results = ev.evaluate(targets)

print(evaluator_results)
## {'Sheet1!B10': ...}

Formula-shape overlay

FormulaEvaluator compiles shape helpers from graph.formula_shapes at construction (an init snapshot). The evaluator holds a shared reference to the graph, but rewarming graph.formula_shapes later does not refresh a live evaluator’s compiled helpers. Construct a new FormulaEvaluator after rewarming if you want the shape path. Missing shapes fall back to Node.formula_ast; the overlay is opt-in and not required for correctness. See Formula-shape overlay.