truecalc-workbook
Workbook layer for the truecalc spreadsheet engine: engine-locked workbook, worksheet, and cell value types with a canonical JSON serialization contract.
Install
[]
= "0.9"
Or via cargo:
Quick start
use ;
// Create a workbook locked to Google Sheets semantics.
let mut wb = new;
// Add a sheet and write some cells.
wb.add_sheet.unwrap;
let a1 = from_a1.unwrap;
let a2 = from_a1.unwrap;
let a3 = from_a1.unwrap;
wb.set.unwrap;
wb.set.unwrap;
wb.set.unwrap;
// Recalculate to evaluate all formulas.
// RecalcContext::new(unix_ms, iana_tz, rng_seed)
let ctx = new.unwrap;
let _changes = wb.recalc;
// Read back the computed result.
assert_eq!;
Design
-
A
Workbookis a value object —Clone + PartialEq + Hash, no hidden state, no callbacks. Mutate via [Workbook::set] / [Workbook::clear], then drive recalc. -
The engine flavor (
sheets|excel) is required at creation and immutable for the workbook's lifetime. It controls formula semantics and the date serial system. -
[
RecalcContext] pins volatile functions (NOW,TODAY) to a fixed UTC instant + IANA timezone via the vendoredchrono-tzdatabase (not the host OS tz tables). Same workbook + same context ⇒ byte-identical recomputed grid. -
[
CellInput] distinguishesLiteral(value)fromFormula("=...".to_string()). Formula syntax is validated against the locked engine atsettime.
Recalc modes
- [
Workbook::recalc] — full recalc, evaluates every formula cell in topological order. - [
Workbook::recalc_incremental] — incremental recalc, recomputes only the transitive dependents of the edited cells (plus all volatile cells). Produces the same result as full recalc.
Both return an ordered list of [Change] values describing every cell that changed.
JSON serialization
[Workbook::to_json] / [Workbook::from_json] implement the canonical RFC 8785 / JCS
serialization boundary — byte-identical output across Rust, WASM, MCP, and REST surfaces.
The JSON schema is the cross-surface contract; see schema/ for the JSON Schema spec.
Schema summary
Key schema invariants:
engineis"sheets"or"excel"— required, immutable.versionis the string"1"— compared by exact match.- Cells without a formula have only
value; formula cells storeformula+ lastvalue. - Named ranges are validated against existing sheets at deserialize time.
Cookbook example
See examples/workbook-budget/ for a worked example
that creates a budget workbook, sets income and expense formulas, recalcs, and prints the
results.
Related crates
truecalc-core: the formula parser and evaluator.
License
MIT, same as the rest of this workspace.