|
| 1 | +--- |
| 2 | +title: Work with formula value preview mode in your custom functions |
| 3 | +description: Work with formula value preview mode in your custom functions |
| 4 | +ms.date: 04/16/2025 |
| 5 | +ms.localizationpriority: medium |
| 6 | +--- |
| 7 | + |
| 8 | +# Work with formula value preview mode in your custom functions (preview) |
| 9 | + |
| 10 | +You can control how your custom function calculates results when participating in formula value preview mode. Formula value preview mode is a feature that allows end users to select portions of a formula while editing the cell to preview the values. This feature helps users evaluate the formula as they edit it. The following image shows an example of the user editing a formula and selecting the text `A1+A2`. The formula preview mode shows the value `7` above. |
| 11 | + |
| 12 | +:::image type="content" source="../images/excel-formula-value-preview.png" alt-text="Screenshot of Excel formula editor with A1+A2 selected and a preview value of 7 displayed above the formula editor."::: |
| 13 | + |
| 14 | +By default, custom functions (for example `=getHousePrice(A1)`) can be previewed by the user. However, the following list shows some scenarios in which you may want to control how your custom function participates in formula value preview mode. |
| 15 | + |
| 16 | +- Your custom function calls one or more APIs that charge a rate for using them. |
| 17 | +- Your custom function accesses one or more scarce resources such as databases. |
| 18 | +- Your custom function takes significant time to calculate the result, and it wouldn’t be useful for a user during preview purposes. |
| 19 | + |
| 20 | +You can change the behavior of your custom function to return a mock value instead. To do this use the `invocation.isInValuePreview` read-only property. The following code sample shows an example custom function named `getHousePrice` that looks up house prices through a monetized API. If `isInValuePreview` is `true`, the custom function returns a mock number to be used and avoids incurring any cost. If `isInValuePreview` is `false`, the custom function calls the API and returns the actual house price value for use in the Excel spreadsheet. |
| 21 | + |
| 22 | +```javascript |
| 23 | +/** |
| 24 | + * Get the listing price for a house on the market for the given address. |
| 25 | + * @customfunction |
| 26 | + * @param address The address of the house. |
| 27 | + * @param invocation Custom function handler. |
| 28 | + * @returns The price of the house at the address. |
| 29 | + */ |
| 30 | +export function getHousePrice(address: string, invocation: CustomFunctions.Invocation): number { |
| 31 | + // Check if this call is for formula value preview mode. |
| 32 | + if (invocation.isInValuePreview) { |
| 33 | + // Avoid long-running expensive service calls. |
| 34 | + // Return a useable but fake number. |
| 35 | + return 450000; |
| 36 | + } else { |
| 37 | + // Make the actual service calls in this block. |
| 38 | + const price = callHouseServiceAPI(address); |
| 39 | + return price; |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +## See also |
| 45 | + |
| 46 | +- [Create custom functions in Excel](/excel/custom-functions-overview.md) |
0 commit comments