Custom Functions and Formula Data
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 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 returns the WorkbookCalculation of a workbook. Its registerFunction method registers a callback, including Promise results, and removeFunction 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:
CustomFunctionDefinitionfor the callback and its argument limits, andCustomFunctionContextfor the context of a call.
In .NET, use the corresponding calculation registration methods and delegate types. Calculation.RegisterFunctionAsync 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. 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 that you set with Workbook.setFormulaProvider.
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 | Per-document function registration, asynchronous settling, external documents and formula auditing in the browser |
registerFunction | Registers a callback, including Promise results. Registration changes invalidate all formula caches. |
removeFunction | Removes a registered callback. Later formulas report #NAME? if no built-in function exists. |
Workbook.setFormulaProvider | Sets the FormulaProvider of the workbook, or null |
FormulaProvider | A function that receives a FormulaRequest and returns a FormulaResult or undefined |
Calculation | Shared calculation settling, external documents and formula dependency auditing in .NET |
Calculation.RegisterFunctionAsync | Registers an asynchronous host function. Results are JSON scalars or arrays, and callbacks cannot reenter the control. |
Calculation.RemoveFunctionAsync | Removes a function and releases its host delegate after invalidating dependent formulas |
CustomFunctionContext (.NET) | The context of a host calculation callback, with the address, the sheet ID and a cancellation token |
Next Steps
- Formulas: write formulas, handle their errors and find the functions that need a provider.
- Events and Cancellation: subscribe to calculation and other workbook events.