> ## Documentation Index
> Fetch the complete documentation index at: https://docs.reducto.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Recalculation

The formula engine is part of the workbook. When you change a cell, the formulas that depend on it are marked dirty. When you read one of them, it is calculated. Nothing is calculated that you do not read.

```ts theme={null}
import { ExcelWorkbook } from "@reductoai/xlsx/officejs";

const book = ExcelWorkbook.create();
const sheet = book.worksheets.getActiveWorksheet();
sheet.getRange("A1:A2").values = [[0.05], [1000]];
sheet.getRange("A3").formulas = [["=A1*A2"]];
console.log(sheet.getRange("A3").values); // [[50]]
sheet.getRange("A1").values = [[0.08]];
console.log(sheet.getRange("A3").values); // [[80]]
```

## Check for errors

Read a formula's value back after you write it. A formula that Microsoft Excel cannot calculate gives the same error here: `#NAME?` for an unknown function or name, `#REF!` for a reference to a deleted cell, `#VALUE!`, `#DIV/0!` and so on.

```ts theme={null}
import { ExcelWorkbook } from "@reductoai/xlsx/officejs";

const book = ExcelWorkbook.fromBytes(bytes);
const used = book.worksheets.getItem("Model").getUsedRange();
used.values.forEach((row, r) =>
  row.forEach((v, c) => {
    if (typeof v === "string" && v.startsWith("#")) console.log(r, c, v, used.formulas[r]![c]);
  }),
);
```

## Calculation mode

`application` controls calculation as the Formulas tab in Microsoft Excel does.

```ts theme={null}
import { ExcelWorkbook } from "@reductoai/xlsx/officejs";

const book = ExcelWorkbook.fromBytes(bytes);
book.application.calculationMode = "Manual";
book.getRange("Inputs!B1").values = [[0.08]];
book.application.calculate("Recalculate");
book.application.calculationMode = "Automatic";

// A file saved without results (written by another tool): compute every formula.
book.application.calculate("Full");

// Circular references: allow them, as File > Options > Formulas does.
book.application.iterativeCalculation.enabled = true;
book.application.iterativeCalculation.maxIteration = 100;
book.application.iterativeCalculation.maxChange = 0.001;
```

## Call a function

`book.functions.call(name, args)` calculates one worksheet function without a cell.

```ts theme={null}
import { ExcelWorkbook } from "@reductoai/xlsx/officejs";

const book = ExcelWorkbook.create();
const payment = book.functions.call("PMT", [0.05 / 12, 360, -200000]);
console.log(payment.value, payment.error); // 1073.64…, null
```

## The core model

`Workbook` from `@reductoai/xlsx` is the model under the facade. It addresses cells by zero-based row and column and reads cached values directly. Use it for bulk reads.

```ts theme={null}
import { Workbook } from "@reductoai/xlsx";

const wb = Workbook.open(bytes);
const ws = wb.getSheet("Inputs")!;
ws.value(0, 1); // B1; rows and columns are zero-based
ws.setValue(0, 1, 0.08);
ws.setFormula(1, 1, "=B1*2");
wb.getSheet("Model")!.value(10, 3); // D11, recalculated on read
const out = wb.save(); // rewrites only edited sheets
```

The engine covers the worksheet functions of Microsoft Excel, including dynamic arrays (`FILTER`, `SORT`, `UNIQUE`, `SEQUENCE`), `LET` and `LAMBDA`. `calc.functionNames()` lists them. See the [Overview](https://docs.reducto.ai/office/xlsx/getting-started/overview.md) for what it does not support.
