Docs
Computed fields & formula functions
Type computed is read-only in fill mode. Its formula string is evaluated for tokens, HTML preview, and PDF. Hosts can extend the formula language with new functions via registerFormulaFunction — without changing the field-type catalog.
Formula language
- Preferred refs:
{Section.FieldName}— same keys assectionsexport. - Table columns:
{Section.TableName.ColumnName}— bare ref joins non-empty values with;. - Legacy:
{fieldId}(e.g.{anamnesis_complaints}). See field id structure. - Operators:
+ - * /(numeric+when both sides are numbers, otherwise string concat). - Circular references are blocked on save and surface as runtime errors.
- Templates store only the formula string — never function implementations.
Examples
// Prefer section + field name (same keys as document export)
{Header.Customer Name}
{Order.Total}
// Table column — all rows; bare ref joins with "; "
{Order.Line Items.Amount}
sum({Order.Line Items.Amount})
avg({Order.Line Items.Qty})
// Names with dots — quote the segment
{Anamnesis."Life history"}
// Legacy — internal field id still works
{header_customer_name}
// Built-in functions & operators
concat({Header.First}, " ", {Header.Last})
age({Patient.Birth Date})
{Order.Subtotal} + {Order.Tax}
count({Order.Line Items.Product}) Display format on the schema (plain / number / currency) only affects presentation; formulas use raw values.
Built-in functions
concat scalar
Any arity — join arguments as strings (null/undefined → '').
age scalar
One ISO date → whole years to today; empty → ''.
sum / avg / min / max / count aggregate
One field or column reference, e.g. sum({Examination.Labs.Value}) — not a comma-separated expression list. No numerics → '' (except count counts non-empty).
Extending with new functions
Computed stays type: 'computed'. Plugins extend the formula language, not field-type plugins. Register implementations in every process that evaluates formulas (editor, n8n, PDF service). Without registration you get Unknown function: <name>.
import { registerFormulaFunction, createEditor } from '@docengine/editor';
// Headless / n8n / PDF: import { registerFormulaFunction } from '@docengine/engine';
// Scalar — comma-separated evaluated args
registerFormulaFunction({
name: 'round',
arity: 1,
label: 'round',
description: 'Round to nearest integer',
impl: ([value]) => Math.round(Number(value)),
});
// Aggregate — one {Field} / column ref; impl receives resolved values
registerFormulaFunction({
name: 'product',
kind: 'aggregate',
description: 'Product of numeric column values',
impl: (values) => {
const nums = values.map(Number).filter((n) => !Number.isNaN(n));
return nums.length ? nums.reduce((a, b) => a * b, 1) : '';
},
});
createEditor({
holder,
// Same process registry (add or override by name)
formulaFunctions: [
{
name: 'ifEmpty',
arity: 2,
impl: ([value, fallback]) =>
value == null || value === '' ? fallback : value,
},
],
});
// Formulas only store the name — never the JS:
// round({Examination.Weight})
// product({Order.Line Items.Qty})
// ifEmpty({Header.Note}, "n/a")FormulaFunctionDef
name—/^[a-zA-Z_][a-zA-Z0-9_]*$/impl(args)— scalar: evaluated args; aggregate: resolved cell/field valueskind—'scalar'(default) or'aggregate'arity— scalar arg count (numberor{ min, max }); ignored for aggregate value arrayslabel/description— formula picker wrap buttons;picker: falsehides the button but still evaluates
Registry rules
- APIs:
registerFormulaFunction,unregisterFormulaFunction,listFormulaFunctions,listFormulaPickerFunctions,resetFormulaFunctions(from@docengine/engine/ editor). - Lookup order: per-call
options.formulaFunctions→ host registry → built-ins. - Registry is process-wide — register once at startup; two editors in one process share (and can overwrite) names.
- Overriding a built-in name replaces it until
unregisterFormulaFunction(name). - Designer wrap buttons come from
listFormulaPickerFunctions()(built-ins + registered).
Per-call overlay (does not mutate the process registry)
evaluateFormula('ifEmpty({n}, "n/a")', values, schemas, {
blocks,
formulaFunctions: [
{ name: 'ifEmpty', arity: 2, impl: ([v, f]) => v || f },
],
});Related: Field types · Custom field types · API & hosts