# Custom Functions and Formula Data

> Register custom formula functions with registerFunction or RegisterFunctionAsync, supply host data with a formula provider and re-register them for a new owner.

- **Product:** TX Sheet Control
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Sheet Control > Spreadsheet Documents > Custom Functions and Formula Data
- **1256 words**
- **Web URL:** https://docs.textcontrol.com/tx-sheet-control/spreadsheets/custom-functions/
- **LLMs URL:** https://docs.textcontrol.com/tx-sheet-control/spreadsheets/custom-functions/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-sheet-control/spreadsheets/custom-functions/llms-full.txt

---

Some calculations belong to the host application rather than to the saved workbook: a function that your application defines, or data that a catalog function needs from your systems. Custom calculation supplies that behavior. Use [`WorkbookCalculation`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbookcalculation/llms-full.txt) and the documented provider contracts, and do not inject executable code into workbook files.

### Register a Custom Function

Register a function with a stable name, supported argument and return types and an explicit synchronous or asynchronous policy, as the function API defines them. In the browser, [`Workbook.calculation`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/calculation-property/llms-full.txt) returns the `WorkbookCalculation` of a workbook. Its [`registerFunction`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbookcalculation/registerfunction-method/llms-full.txt) method registers a callback, including Promise results, and [`removeFunction`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbookcalculation/removefunction-method/llms-full.txt) removes it.

- **Determinism.** Keep callbacks deterministic where possible.
- **Validation.** Validate argument values, array dimensions and error results.
- **Typed contracts.** Typed contracts describe the function context, values, references and cancellation: [`CustomFunctionDefinition`](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/customfunctiondefinition/llms-full.txt) for the callback and its argument limits, and [`CustomFunctionContext`](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/customfunctioncontext/llms-full.txt) for the context of a call.

In .NET, use the corresponding calculation registration methods and delegate types. [`Calculation.RegisterFunctionAsync`](https://docs.textcontrol.com/tx-sheet-control/reference/dotnet-api/txtextcontrol-txsheetcontrol-server/calculation/registerfunctionasync-method/llms-full.txt) takes the name of the function and the callback, a `Func` delegate that returns a `JsonElement` or a `Task<JsonElement>`. Its options are of the type [`CustomFunctionOptions`](https://docs.textcontrol.com/tx-sheet-control/reference/dotnet-api/txtextcontrol-txsheetcontrol-server/customfunctionoptions/llms-full.txt). Read their return and exception contracts before adapting an arbitrary service callback.

### Supply Data With a Formula Provider

Some catalog functions require application data or Excel-specific objects. Fetch the external data asynchronously in the host and cache it. When the provider contract requires a synchronous result, return a supported synchronous value or error from the provider, a [`FormulaProvider`](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/formulaprovider/llms-full.txt) that you set with [`Workbook.setFormulaProvider`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/setformulaprovider-method/llms-full.txt).

> Warning
>
> **No Promise From a Synchronous Provider.** Returning a promise to a synchronous provider is invalid.

Missing providers and unsupported results produce defined formula errors rather than automatic service calls. Formula text containing a filename, URL or external function does not grant network or filesystem access.

### How Long Registrations Last

Registrations are per application, control or workbook configuration, and they are not serialized as executable code. Re-register them after creating a new owner. Loading document data should not be confused with loading application callbacks.

Refresh providers and data deliberately, so that calculation caches are invalidated according to the supported API.

### Key Members

| Member | Purpose |
|---|---|
| [`WorkbookCalculation`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbookcalculation/llms-full.txt) | Per-document function registration, asynchronous settling, external documents and formula auditing in the browser |
| [`registerFunction`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbookcalculation/registerfunction-method/llms-full.txt) | Registers a callback, including Promise results. Registration changes invalidate all formula caches. |
| [`removeFunction`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbookcalculation/removefunction-method/llms-full.txt) | Removes a registered callback. Later formulas report `#NAME?` if no built-in function exists. |
| [`Workbook.setFormulaProvider`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/setformulaprovider-method/llms-full.txt) | Sets the `FormulaProvider` of the workbook, or `null` |
| [`FormulaProvider`](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/formulaprovider/llms-full.txt) | A function that receives a [`FormulaRequest`](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/formularequest/llms-full.txt) and returns a [`FormulaResult`](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/formularesult/llms-full.txt) or `undefined` |
| [`Calculation`](https://docs.textcontrol.com/tx-sheet-control/reference/dotnet-api/txtextcontrol-txsheetcontrol-server/calculation/llms-full.txt) | Shared calculation settling, external documents and formula dependency auditing in .NET |
| [`Calculation.RegisterFunctionAsync`](https://docs.textcontrol.com/tx-sheet-control/reference/dotnet-api/txtextcontrol-txsheetcontrol-server/calculation/registerfunctionasync-method/llms-full.txt) | Registers an asynchronous host function. Results are JSON scalars or arrays, and callbacks cannot reenter the control. |
| [`Calculation.RemoveFunctionAsync`](https://docs.textcontrol.com/tx-sheet-control/reference/dotnet-api/txtextcontrol-txsheetcontrol-server/calculation/removefunctionasync-method/llms-full.txt) | Removes a function and releases its host delegate after invalidating dependent formulas |
| [`CustomFunctionContext`](https://docs.textcontrol.com/tx-sheet-control/reference/dotnet-api/txtextcontrol-txsheetcontrol-server/customfunctioncontext/llms-full.txt) (.NET) | The context of a host calculation callback, with the address, the sheet ID and a cancellation token |

### Next Steps

- [Formulas](https://docs.textcontrol.com/tx-sheet-control/spreadsheets/formulas/llms-full.txt): write formulas, handle their errors and find the functions that need a provider.
- [Events and Cancellation](https://docs.textcontrol.com/tx-sheet-control/spreadsheet-editor/events/llms-full.txt): subscribe to calculation and other workbook events.

---

## Nearby

- **Previous:** [Formulas](https://docs.textcontrol.com/tx-sheet-control/spreadsheets/formulas/llms-full.txt)
- **Next:** [Named Ranges and External References](https://docs.textcontrol.com/tx-sheet-control/spreadsheets/named-ranges/llms-full.txt)
