Function compileFormula

  • Compiles a formula once – no eval, no new Function (works with a strict Content-Security-Policy, user input cannot run code). The result evaluates rows; never throws.

    Syntax (details: instructions.md „Formulas“):

    • fields in brackets: [price], [address.city], [Unit price] – own properties of the row or calculated columns
    • numbers 12, 1.5, 1e3, texts "a" / 'a', TRUE, FALSE
    • operators: + - * / ^ (power), & (text), = <> < <= > >=; -2^2 = -4
    • functions: ROUND(x, 2), IF(c, a, b), IFERROR, AND, OR, NOT, ABS, MOD, SQRT, POWER, FLOOR, CEILING, CONCAT, LEN, UPPER, LOWER, TRIM, ISBLANK; SUM AVG MIN MAX COUNT – with one field the aggregate of the column ([total] / SUM([total])), otherwise of the arguments

    Parameters

    • source: string

      the formula, e.g. "[price] * [quantity] * 1.19" (a leading = is ignored)

    • Optional options: FormulaOptionsIf

      own functions

    Returns CompiledFormula

    the compiled formula

    Example

    const gross = compileFormula("ROUND([price] * [quantity] * 1.19, 2)");
    gross.evaluate({ price: 25, quantity: 10 }); // 297.5
    compileFormula("[price] / 0").evaluate({ price: 1 }); // FormulaError #DIV/0!
    compileFormula("[price] *").error?.code; // "#ERROR!"

    Since

    1.1.95