Custom Functions (Rust, Python, JS)
Register workbook-local callbacks with consistent semantics across Rust, Python, and JS/WASM.
Formualizer exposes the same custom-function model in all runtimes: register a workbook-local callback, call it from formulas, and unregister when done.
Semantics that are the same across runtimes
- Function names are case-insensitive and canonicalized to uppercase (
my_fn,MY_FN,My_Fnall map to one entry). - Resolution is workbook-local first, then global built-ins.
- Built-in override is blocked by default; opt in with
allow_override_builtin(Rust/Python) orallowOverrideBuiltin(JS). - Arity is validated before callback execution (
min_args/max_argsstyle options). - Range arguments are materialized as 2D arrays (
LiteralValue::Arrayin Rust, nested lists in Python, nested arrays in JS). - Returning an array spills into the grid using normal dynamic-array behavior.
Register and call a custom function
use formualizer_common::LiteralValue;use formualizer_workbook::{CustomFnOptions, Workbook};use std::sync::Arc;let mut wb = Workbook::new();wb.add_sheet("Sheet1")?;wb.set_values( "Sheet1", 1, 1, &[ vec![LiteralValue::Number(1.0), LiteralValue::Number(2.0)], vec![LiteralValue::Number(3.0), LiteralValue::Number(4.0)], ],)?;wb.register_custom_function( "range_total", CustomFnOptions { min_args: 1, max_args: Some(1), ..Default::default() }, Arc::new(|args: &[LiteralValue]| { let total = match &args[0] { LiteralValue::Array(rows) => rows.iter().flatten().fold(0.0, |acc, v| { acc + match v { LiteralValue::Number(n) => *n, LiteralValue::Int(i) => *i as f64, _ => 0.0, } }), _ => 0.0, }; Ok(LiteralValue::Number(total)) }),)?;wb.set_formula("Sheet1", 1, 3, "=RANGE_TOTAL(A1:B2)")?;assert_eq!(wb.evaluate_cell("Sheet1", 1, 3)?, LiteralValue::Number(10.0));Quick verification commands
cargo run -p formualizer-workbook --example custom_function_registration
python bindings/python/examples/custom_function_registration.py
cd bindings/wasm && npm run build && node examples/custom-function-registration.mjsRelated
Rust panic handling
On native builds with unwinding enabled, a Rust panic in a builtin or registered function becomes #VALUE! with ExcelErrorExtra::FunctionPanicked { function, message }. The canonical function name and a string panic payload are retained; non-string payloads have no message. Workbook callbacks use the same payload and the message "Function callback panicked". An ordinary returned error keeps its existing behavior.
IFERROR(PANICKING(), 0) may return 0, while operation diagnostics still report the executed panic. Source recalculation therefore reports Partial coverage and rejects requireComplete, even when the final cell value hides the error. Function panics leave the engine usable; dependent cells see the ordinary error and a later edit can recalculate normally.
Engine-internal panics propagate and permanently poison the engine. Check Engine::is_poisoned() or Workbook::is_poisoned(); reads may be stale, and further evaluation or mutation is refused. Discard that engine. See interruptible recalculation for the refusal payloads.
Python exceptions and JavaScript throws already become ordinary callback errors. Rust panics on wasm32-unknown-unknown trap instead of unwinding, so these native catches do not provide recovery there; discard the affected workbook/worker or instance after a trap.
Function metadata and resolver callbacks are host code, too. Runtime hooks
(caps, reporting/semantic/dependency contracts, schemas/arity, family-kernel
selection and format metadata) are caught at their invocation sites. A planning
panic declines optimization with conservative capabilities and marks that
function instance for the rest of the logical request, including later job
steps. Every executed root of that instance receives the typed function-panic
error and diagnostic. Untaken calls do not create execution diagnostics.
A subsequent request retries the metadata; no function metadata panic poisons
the engine.
Resolver failures reuse FunctionPanicked with explicit names such as
resolver:resolve_source_scalar:ReviewSource and
resolver:resolve_source_table:Sales. The source attribution is retained inside
ABS(ReviewSource); it is not attributed to ABS. Version/provider callbacks
and returned table/range methods have the same narrow containment. The catch
finishes inside source-cache guards, and failed values are not cached.
Ordinary returned errors retain their existing behavior.
A panicking clock provider freezes a failed sample for that logical
recalculation. Only roots reading the clock (including NOW/TODAY) receive
FunctionPanicked attributed to resolver:clock and the diagnostic; unrelated
formulas are unaffected. IFERROR can catch the value. Jobs do not resample a
failed clock mid-job; the next request samples again. Deterministic clocks keep
their existing behavior.
Deferred source replay callbacks are also contained inside their spool guards. Legacy replay failures return a typed request error; engine checkpoint panics still unwind and trigger the internal backstop. CalcObserver is currently a dormant trait without any engine registration or invocation path, so no observer notifications are delivered. Progress callbacks still run between steps; their panics propagate without poisoning or changing the already-reached job state.
Iterative Calculation (Excel Parity)
Enable Excel-style iterative calculation for convergent cycles, accumulators, and self-referential timestamps, with convergence semantics and XLSX calcPr round-trip.
WASM Plugins: Inspect, Attach, Bind
Use the Rust workbook plugin seam to inspect manifests, attach modules, and bind exports to worksheet functions.