> ## 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.

# Formulas

Write the formula, then read the value back.

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

const book = ExcelWorkbook.create();
const sheet = book.worksheets.getActiveWorksheet();
sheet.name = "Sales";
sheet.getRange("A1:C3").values = [
  ["Region", "Q1", "Q2"],
  ["North", 10, 12],
  ["South", 8, 15],
];
sheet.getRange("D1").values = [["Total"]];
sheet.getRange("D2:D3").formulas = [["=SUM(B2:C2)"], ["=SUM(B3:C3)"]];
sheet.getRange("B2:D3").numberFormat = "#,##0";

const total = book.getRange("Sales!D2");
console.log(total.values, total.text); // [[22]] [["22"]]
await book.save("sales.xlsx");
```

`values` is the calculated value; `text` is the value as the cell shows it, with its number format.

## Defined names

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

const book = ExcelWorkbook.create();
const sheet = book.worksheets.getActiveWorksheet();
sheet.getRange("B1").values = [[0.08]];
book.names.add("Rate", sheet.getRange("B1"));
sheet.getRange("B2").formulas = [["=Rate*3"]];
console.log(sheet.getRange("B2").values); // [[0.24]]
```

## Errors

An unknown function or name gives `#NAME?`, as in Microsoft Excel. Read the value back to find it:

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

const book = ExcelWorkbook.create();
const cell = book.worksheets.getActiveWorksheet().getRange("A1");
cell.formulas = [["=SUMM(1,2)"]];
console.log(cell.values); // [["#NAME?"]]
```

> [!WARNING]
>
> Do not write a number you computed in place of a formula. The file looks right, but it does not update when an input changes.
