Formualizer Docs
Reference

JS/WASM API Reference

Comprehensive documentation for the Formualizer JavaScript and WebAssembly bindings.

The formualizer npm package provides high-performance formula parsing and evaluation by leveraging a WebAssembly core compiled from Rust.

Initialization

Before using the library, you must initialize the WebAssembly module.

import init, { initializeWasm } from 'formualizer';

// Initialize the module
await init();
// or
await initializeWasm();

Tokenizer

The Tokenizer class allows you to break a formula into its constituent parts without parsing the full structure.

  • constructor(formula: string, dialect?: FormulaDialect)
  • tokens: Read-only array of Token objects.
  • render(): Returns the formula string reconstructed from tokens.
  • length: Returns the number of tokens.
  • getToken(index: number): Returns the Token at the specified index.

Parser

The Parser class handles the conversion of a formula string into an Abstract Syntax Tree (AST).

  • constructor(formula: string, dialect?: FormulaDialect)
  • parse(): Returns ASTNodeData representing the root of the AST.

Convenience Functions

For quick operations, use these standalone functions:

  • tokenize(formula: string, dialect?: FormulaDialect): Returns an array of tokens.
  • parse(formula: string, dialect?: FormulaDialect): Returns the root ASTNodeData.

Workbook API

The Workbook class manages stateful evaluation across multiple sheets and cells.

  • constructor(options?: WorkbookLoadOptions): Create a new empty workbook.
  • fromJson(json: string, options?: WorkbookLoadOptions): Load workbook state from a JSON string.
  • fromXlsxBytes(bytes: Uint8Array | ArrayBufferLike, options?: WorkbookLoadOptions): Load workbook state from XLSX bytes.
  • addSheet(name: string): Add a new sheet to the workbook.
  • setValue(sheet: string, row: number, col: number, value: any): Set a literal value.
  • setFormula(sheet: string, row: number, col: number, formula: string): Set a formula.
  • evaluateCell(sheet: string, row: number, col: number): Evaluate a specific cell.
  • evaluateAll(): Re-evaluate all dirty cells in the workbook.
  • sheet(name: string): Returns a sheet facade with helper methods like setValues and setFormulas.

Recalculation jobs

APIBehavior
wb.beginRecalc(options?)Returns the generated RecalcJob runtime class with typed methods; defaults: collectDeltas: true, maxDeltaRecords: 1024, no priority
job.step(budgetMs?)Detached StepReport; null/undefined means no soft deadline; zero progresses
job.prioritize(ranges)Replaces queued priority with bounded, 1-based inclusive rectangles
job.active, job.cancel()Query activity or terminate this generation, retaining installed results
wb.hasActiveRecalcJobWhether an engine job is active
wb.hasPendingInRect(area), wb.pendingCellsInRect(area)Bounded viewport query; pending cells use { sheet, row, column }
wb.recalc({ budgetMs = 5, onStep, ...jobOptions }?)Synchronous driver; onStep(report, job) === false pauses
wb.evaluateWithYield({ budgetMs = 5, onProgress, signal, yieldControl, ...jobOptions }?)Awaited progress and task turns; abort rejects AbortError

RecalcOutcome is a discriminated union: { status: 'Finished', result }, { status: 'Paused', job }, or { status: 'Cancelled' }. The async driver never pauses from callback return values. Both drivers hold exclusive ownership through terminal callbacks and cancel their owned generation on hook failure while preserving the original exception.

StepReport.phase is BuildingGraph, Scheduling, Evaluating, Finished or Cancelled. Only Finished has a non-null result (computedVertices, cycleErrors, elapsedMs). Every report includes progress, stepComputed, stepElapsedMs, changed, overflowSheets and nullable longestUnit. progress.totalExact describes whether the current pass total is proven fixed; progress can replan after edits.

Exported types include RecalcJobOptions, RecalcOptions, EvaluateWithYieldOptions, RecalcJob, StepReport, RecalcProgress, RecalcLongestUnit, RecalcResult, RecalcOutcome, RecalcJobError, RecalcEvaluationCause and RecalcJobActiveError. Job errors expose stable code fields, including ALREADY_PENDING with boolean explicit and EVALUATION with structured cause. Ordinary evaluation refusal preserves its existing error kind and adds code: 'RECALC_JOB_ACTIVE'.

See Interruptible Recalculation for cancellation, resource limits, atomic step overruns, ownership and runnable examples. Internal generated bridge helpers are reserved for the facade.

Worker Orchestration (formualizer/worker)

The subpath export formualizer/worker provides a zero-WASM client proxy (createWorkbookWorker), a worker-side host dispatcher (attachWorkbookWorker), and an in-process MessageChannel pair helper (createInProcessWorkbookWorker). A browser module worker entry point is exported as formualizer/worker-entry.

APIBehavior
createWorkbookWorker(endpoint, options?)Connect a client to a DOM Worker/MessagePort or Node worker_threads Worker/MessagePort; supports initial source (empty, json, xlsx), autoRecalc, autoRecalcBudgetMs, maxDeltaRecords, terminateOnDispose
attachWorkbookWorker(endpoint, options?)Attach the worker host to self, parentPort, or a MessagePort; accepts pre-loaded runtime, worker-side functions, allowFunctionFromSource, clearFunctionsOnLoad, and importModule
createInProcessWorkbookWorker(options?)Create an in-process MessageChannel host + client pair
client.state, client.hasInFlightMutations, client.onStatusChange(fn)Inspect { revision, evaluatedRevision, ackClientSeq, recalcStatus, hasActiveJob } and causal optimistic-edit state
client.loadEmpty, client.loadFromJson, client.loadFromXlsxBytesReset/reload the worker workbook (revision resets to 0, ackClientSeq advances, transfer: true supported for XLSX bytes)
client.setValue, client.setFormula, client.setValues, client.setFormulas, client.editBatchMutate cells or run ordered batches (with optional actionDescription undo grouping), advancing revision and scheduling autoRecalc when enabled
client.getValue, client.getValueWithState, client.getFormula, client.readRange, client.readRangeWithStateRead cached values or { data, ...state } without creating missing sheets
client.subscribeViewport(options)Subscribe a 1-based inclusive rectangle in values (grid or patches), snapshots, deltas, or transferable columnar (tags, numbers, strings, pendingBitset) mode with automatic job priority and credit-based flow control
client.recalc, client.recalcStream, client.beginRecalc, client.stepRecalc, client.pauseRecalc, client.cancelRecalc, client.prioritizeDrive or stream interruptible recalculation across the worker boundary with AbortSignal and pause/inspect/repair/resume support
client.evaluateAll, client.evaluateCell, client.evaluateCells, client.createSheetPortSession, client.evaluateSheetPort, client.recalculateXlsxBytesOne-shot evaluation, stateful/stateless SheetPort, and transferable cache-only XLSX recalculation (automatically coordinating with implicit autoRecalc jobs)
client.registerFunctionModule, client.registerFunctionFromSource, client.unregisterFunction, client.listFunctionsRegister and manage worker-side custom functions
client.dispose(), client[Symbol.asyncDispose]()Free worker jobs, sessions, cached sheet handles, and workbook; terminates endpoint unless terminateOnDispose: false

Custom Functions

Extend the engine with your own logic:

  • registerFunction(name: string, callback: Function, options?): Add a custom function.
  • unregisterFunction(name: string): Remove a custom function.
  • listFunctions(): Get a list of all registered functions.

SheetPort

SheetPortSession provides a high-level API for interacting with workbooks via defined inputs and outputs (ports).

  • fromManifest(manifest: FioManifest, workbook: Workbook): Create a session from a manifest object.
  • fromManifestYaml(yaml: string, workbook: Workbook): Create a session from a YAML manifest.
  • defineManifest(input): Build a FioManifest from the short port form (location as an A1 string, type for a single cell); the Fio* types describe the canonical manifest.
  • readInputs(): Get current values for all input ports.
  • readOutputs(): Get current values for all output ports.
  • writeInputs(update: object): Update input port values.
  • evaluateOnce(options?): Run a single evaluation cycle.

Interfaces and Types

Token

interface Token {
  tokenType: string;
  subtype: string;
  value: string;
  pos: number;
  end: number;
}

ASTNodeData

interface ASTNodeData {
  type: string;
  value?: any;
  reference?: ReferenceData;
  name?: string;
  args?: ASTNodeData[];
  op?: string;
  left?: ASTNodeData;
  right?: ASTNodeData;
  operand?: ASTNodeData;
  elements?: ASTNodeData[];
  message?: string;
}

FormulaDialect

  • Excel: Standard Excel formula syntax.
  • OpenFormula: OpenDocument formula syntax.

See Also

On this page