Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api/api_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Parameters:
| Name | Description |
| :------------------------------------------- | :-------------------------------------------------- |
| [](api/spreadsheet_addcolumn_method.md) | @getshort(api/spreadsheet_addcolumn_method.md) |
| [](api/spreadsheet_addformula_method.md) | @getshort(api/spreadsheet_addformula_method.md) |
| [](api/spreadsheet_addrow_method.md) | @getshort(api/spreadsheet_addrow_method.md) |
| [](api/spreadsheet_addsheet_method.md) | @getshort(api/spreadsheet_addsheet_method.md) |
| [](api/spreadsheet_clear_method.md) | @getshort(api/spreadsheet_clear_method.md) |
Expand Down
1 change: 1 addition & 0 deletions docs/api/overview/methods_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ description: You can have a Methods overview of the DHTMLX JavaScript Spreadshee
| Name | Description |
| :------------------------------------------ | :------------------------------------------------- |
| [](../spreadsheet_addcolumn_method.md) | @getshort(../spreadsheet_addcolumn_method.md) |
| [](../spreadsheet_addformula_method.md) | @getshort(../spreadsheet_addformula_method.md) |
| [](../spreadsheet_addrow_method.md) | @getshort(../spreadsheet_addrow_method.md) |
| [](../spreadsheet_addsheet_method.md) | @getshort(../spreadsheet_addsheet_method.md) |
| [](../spreadsheet_clear_method.md) | @getshort(../spreadsheet_clear_method.md) |
Expand Down
51 changes: 51 additions & 0 deletions docs/api/spreadsheet_addformula_method.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
sidebar_label: addFormula()
title: addFormula method
description: You can learn about the addFormula method in the documentation of the DHTMLX JavaScript Spreadsheet library. Browse developer guides and API reference, try out code examples and live demos, and download a free 30-day evaluation version of DHTMLX Spreadsheet.
---

# addFormula()

### Description

@short: Registers a custom formula function that can be used in cell formulas

Once registered, the formula is available in any cell by its uppercase name (e.g. =MYFUNC(A1, B2)).

### Usage

~~~jsx
addFormula(name: string, handler: (...args: any[]) => any): void;
~~~

### Parameters

- `name` - (*string*) required, the formula name (case-insensitive, stored as uppercase)
- `handler` - (*(...args: any[]) => any*) required, the function that computes the formula result. Receives the resolved cell values as arguments

:::note
The `handler` callback function must be synchronous. Using `Promise` or `fetch` inside the function is not allowed.
:::

### Example

~~~jsx {4-6}
const spreadsheet = new dhx.Spreadsheet("spreadsheet_container", {});

// Adds a custom formula that doubles a value
spreadsheet.addFormula("DOUBLE", (value) => {
return value * 2;
});

// Now use in cells: =DOUBLE(A1)
spreadsheet.parse([
{ cell: "A1", value: 4, format: "number" },
{ cell: "B1", value: "=DOUBLE(A1)", format: "number" }
]);
~~~

**Change log:** Added in v6.0

**Related sample:** [Spreadsheet. Custom formula](https://snippet.dhtmlx.com/wvxdlahp)

**Related articles:** [Formulas and functions](/functions/#custom-formulas)
29 changes: 28 additions & 1 deletion docs/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -1326,4 +1326,31 @@ When you enter a formula, a popup with description of the function and its param

Check the example in our [snippet tool](https://snippet.dhtmlx.com/wux2b35b).

You can modify the default locale for the popup with formula parameters and add a custom locale. Check the details in the [Localization](localization.md/#default-locale-for-formulas) guide.
You can modify the default locale for the popup with formula parameters and add a custom locale. Check the details in the [Localization](localization.md/#default-locale-for-formulas) guide.

## Custom formulas

Starting with v6.0, you can register custom formula functions via the [`addFormula()`](api/spreadsheet_addformula_method.md) method. Once registered, the formula is available in any cell by its uppercase name.

The method takes two parameters: the formula name and a synchronous handler function that receives the resolved cell values as arguments and returns the result:

~~~js
spreadsheet.addFormula("DOUBLE", (value) => {
return value * 2;
});
~~~

After that, the formula can be used in cells just like any built-in function:

~~~js
spreadsheet.parse([
{ cell: "A1", value: 4, format: "number" },
{ cell: "B1", value: "=DOUBLE(A1)", format: "number" }
]);
~~~

:::note
The handler function must be synchronous. Using `Promise` or `fetch` inside the function is not allowed.
:::

**Related sample:** [Spreadsheet. Custom formula](https://snippet.dhtmlx.com/wvxdlahp)
1 change: 1 addition & 0 deletions sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module.exports = {
},
items: [
"api/spreadsheet_addcolumn_method",
"api/spreadsheet_addformula_method",
"api/spreadsheet_addrow_method",
"api/spreadsheet_addsheet_method",
"api/spreadsheet_clear_method",
Expand Down