Skip to content

Calculation settings and iteration ​

Calculation settings are workbook state that decides how the engine computes: the calc mode, the other <calcPr> options, the Compatibility Version, and iterative calculation for intentional cycles.

Calculation settings and file persistence ​

Calc mode (wb.getCalcMode() / wb.setCalcMode(), with CalcMode.Auto, Manual, or AutoNoTable; get_calc_mode() / set_calc_mode() in Python) and iterative settings are workbook state while the workbook is open, and the mode is saved in the file. The mode decides when dirty cells are computed, as When calculation runs describes. A workbook loaded with Manual stays manual, so reading it returns its stored values until recalc().

The other <calcPr> settings have typed accessors too, and every attribute of the element is kept across a resave:

SettingJavaScript (wb)Python (wb)
Set precision as displayed (fullPrecision; metadata only)getFullPrecision() / setFullPrecision(value)get_full_precision() / set_full_precision(value)
Recalculate before saving under Manual mode (calcOnSave, default on)getCalcOnSave() / setCalcOnSave(value)get_calc_on_save() / set_calc_on_save(value)
Reference notation (refMode)getRefMode() / setRefMode(RefMode.A1 | RefMode.R1C1)get_ref_mode() / set_ref_mode(mode)
1904 date system (<workbookPr date1904>)getDate1904() / setDate1904(value)get_date1904() / set_date1904(value)
Compatibility VersiongetCompatibilityVersion() / setCompatibilityVersion(version)get_compatibility_version() / set_compatibility_version(version)

The flag getters return boolean (bool in Python), and the reference-notation getter returns a RefMode. setDate1904() switches the date system without converting stored serials, so every date shifts by 1462 days.

XLSX stores these in <calcPr> and <workbookPr>. The XLSB writer emits them as BrtCalcProp and BrtWbProp, so manual calculation and iteration settings survive a binary save. See File formats for what XLSB leaves out.

Compatibility Version ​

Excel's Compatibility Version decides how LEN, MID, FIND, SEARCH and REPLACE count positions in text. Under version 2 they count characters, so a character outside the Basic Multilingual Plane, such as an emoji or a rare CJK ideograph, is one position. Under version 1 they count UTF-16 units, and such a character is two. LEFT and RIGHT count it as one position under every version, as Excel does. The rule is the same on every profile, Mac and Windows alike.

Formula, with A1 holding a😀b😁cVersion 2Version 1
=LEN(A1)57
=FIND("b",A1)34
=MID(A1,2,3)😀b😁😀b
=LEFT(A1,3)a😀ba😀b

wb.getCompatibilityVersion() (Python get_compatibility_version()) returns a CompatibilityVersion: Version1, Version2 or Version3 (Python VERSION_1 to VERSION_3), whose values 1 to 3 are the numbers the file stores.

  • A new workbook is version 2.
  • A loaded workbook whose file carries no version is version 1, as in Excel, and getCompatibilityVersion() returns Version1 for it; such files include those written by other tools and those Excel saved under version 1.
  • Version 3 adds nested arrays, which Formulon does not implement, and evaluates as version 2.
  • A number above 3, stamped by a later Excel, is returned as stored (a plain number in JavaScript, a CompatibilityVersion value named VERSION_<n> in Python), evaluates as version 2 and is written back unchanged.

setCompatibilityVersion(version) marks every formula dirty only when the change crosses between version 1 and the other two; setting the current value changes nothing, and a value outside the enum, including a number above 3 read back from the getter, fails with StatusCode.InvalidArgument and leaves the workbook unchanged.

Both .xlsx and .xlsb read and write the version. A save writes what the workbook holds: a loaded file keeps the version it carries, a file without one stays without one, and a new workbook is saved with version 2.

js
import createFormulon, { CompatibilityVersion } from '@libraz/formulon'

const formulon = await createFormulon()
using wb = formulon.Workbook.createDefault()
const ws = wb.sheet(0)
ws.setValue('A1', 'a😀b😁c')
ws.setFormula('B1', '=LEN(A1)')
console.log(wb.getCompatibilityVersion(), ws.getValue('B1')) // 2 5
wb.setCompatibilityVersion(CompatibilityVersion.Version1)
console.log(ws.getValue('B1')) // 7
python
from formulon import CompatibilityVersion, Workbook

with Workbook.create_default() as wb:
    ws = wb[0]
    ws["A1"] = "a😀b😁c"
    ws["B1"] = "=LEN(A1)"
    print(int(wb.get_compatibility_version()), ws["B1"])  # 2 5.0
    wb.set_compatibility_version(CompatibilityVersion.VERSION_1)
    print(ws["B1"])  # 7.0

Under version 1, a MID or REPLACE cut that falls inside a two-unit character takes the whole character, where Excel returns half of it.

When Mac Excel opens a file that carries no version and has no cached values, it calculates under version 2 until the next full recalculation. Formulon does not recalculate at open, so cached values stay as written, and computes such a file as version 1 on its first recalculation (Remaining differences).

Iterative calculation ​

Workbooks with intentional cycles — interest accrual, goal-seek style fixed points — need iterative calculation. The solver sweeps independent cyclic components together and applies a workbook-wide change test, stopping when the maximum change falls below the configured tolerance or the iteration cap is hit.

js
import createFormulon from '@libraz/formulon'

const formulon = await createFormulon()
using wb = formulon.Workbook.createDefault()
const ws = wb.sheet(0)
// A1 and B1 read each other: A1 = B1 / 2 + 1 settles at 2.
ws.setFormula('A1', '=B1*0.5+1')
ws.setFormula('B1', '=A1')
wb.updateIterative({ enabled: true, maxIterations: 100, maxChange: 0.001 })
let sweeps = 0
wb.setIterativeProgress(() => {
  sweeps += 1
  return true // false aborts the solve
})
wb.recalc()
console.log(wb.getIterative()) // { enabled: true, maxIterations: 100, maxChange: 0.001 }
const a1 = ws.getValue('A1')
console.log(sweeps > 0, typeof a1 === 'number' && a1.toFixed(2)) // true 2.00
python
from formulon import Workbook

with Workbook.create_default() as wb:
    ws = wb[0]
    # A1 and B1 read each other: A1 = B1 / 2 + 1 settles at 2.
    ws["A1"] = "=B1*0.5+1"
    ws["B1"] = "=A1"
    wb.update_iterative(enabled=True, max_iterations=100, max_change=0.001)
    wb.recalc()
    print(wb.get_iterative())  # IterativeSettings(enabled=True, max_iterations=100, max_change=0.001)
    print(f"{ws['A1']:.2f}")  # 2.00

wb.setIterativeProgress(callback) registers a callback, called as (iteration, maxResidual, maxIterations), that fires after every sweep over the cyclic cells; null removes it.

The limits themselves are fields of the updateIterative() patch, and an omitted field keeps its stored value, so wb.updateIterative({ enabled: false }) toggles iteration while keeping the limits. Python's update_iterative() takes the same three fields as keyword-only arguments.

The callback is JavaScript-only. It runs while the solver holds the workbook mid-sweep, so it must not call any method on that workbook: such a call, a read included, throws FormulonError with code === StatusCode.GraphRecalcReentrant (4005) and changes nothing.

wb.getIterative() returns { enabled, maxIterations, maxChange }; Python's get_iterative() returns an IterativeSettings record with enabled, max_iterations, and max_change. maxIterations must be in 1..32767: 0 or a larger value throws InvalidArgument and is not stored. A loaded workbook that declares a larger iterateCount reads back as 32767.

A maxChange that is not finite throws FormulonError with StatusCode.InvalidArgument. A negative maxChange fails the same way, and the stored value stays.

A bad maxIterations fails as follows:

ValueJavaScriptPython
Wrong typeTypeError for one that is not a NumberTypeError for a float or bool
Fractional, non-finite or beyond 32 bitsRangeError for a fractional or non-finite one or one beyond 32 bitsValueError for an integer beyond 32 bits
0, negative or above 32767StatusCode.InvalidArgument (Workbook.updateIterative maxIterations must be in 1..32767)InvalidArgumentError

Cycles outside iteration need inspection

If iterative calculation is off, a cycle containing a single formula cell (for example, =A1+1 in A1) holds 0; a cycle involving multiple cells produces #REF!. A cycle that closes through dynamic references such as OFFSET or INDIRECT may retain its prior cached values instead. These results are not host exceptions: recalc() returns normally. Turn iteration on explicitly when cycles are intentional.

Finding the cells in a cycle ​

ws.listCircularReferences() (Python list_circular_references()) returns the cells of the sheet that sit in an unresolved circular reference, as { row, col } in row-major order, and an empty list when there are none.

Excel's status bar names one cell of a cycle, and which one depends on the order the formulas were entered, which a workbook does not record, so Formulon lists every member.

A cell that only reads a cycle is not listed. A cycle that crosses sheets is listed on each sheet for that sheet's own cells. Under Auto the call calculates first; while iterative calculation is on, nothing is listed. Call it when the list is shown rather than on every render.

js
import createFormulon from '@libraz/formulon'

const formulon = await createFormulon()
using wb = formulon.Workbook.createDefault()
const ws = wb.sheet(0)
ws.setFormula('C2', '=A1')
ws.setFormula('A1', '=B3')
ws.setFormula('B3', '=C2')
ws.setFormula('D1', '=A1+1') // reads the cycle, is not part of it
console.log(ws.listCircularReferences().map(({ row, col }) => [row, col])) // [ [ 0, 0 ], [ 1, 2 ], [ 2, 1 ] ]
wb.updateIterative({ enabled: true })
console.log(ws.listCircularReferences().length) // 0
python
from formulon import Workbook

with Workbook.create_default() as wb:
    ws = wb[0]
    ws["C2"] = "=A1"
    ws["A1"] = "=B3"
    ws["B3"] = "=C2"
    ws["D1"] = "=A1+1"  # reads the cycle, is not part of it
    print([(c.row, c.col) for c in ws.list_circular_references()])  # [(0, 0), (1, 2), (2, 1)]
    wb.update_iterative(enabled=True)
    print(len(ws.list_circular_references()))  # 0

The panel below solves a two-cell cycle with those same three arguments exposed. Tighten maxChange and the solve takes more sweeps to get under it; set an iteration cap below what the tolerance needs and the solve stops early, reporting the run as not converged with its last residual still above the guide line.

Circular references and iterative calculation

Two mutually referencing cells are solved iteratively. The curve is the residual reported by setIterativeProgress() after each sweep, plotted exactly as the engine streamed it.

Powered by the real Formulon engine (WASM) — it runs entirely in your browser, nothing is uploaded.