Skip to content

Commit 2aa0a59

Browse files
[Excel](custom functions) Support formula value preview (#5129)
* Formula value preview for custom functions * move file to correct location * Apply suggestions from code review Co-authored-by: Alison McKay <[email protected]> * update from feedback --------- Co-authored-by: Alison McKay <[email protected]>
1 parent 0ffdea2 commit 2aa0a59

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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)
22.6 KB
Loading

docs/toc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,9 @@ items:
619619
- name: Data types
620620
href: excel/custom-functions-data-types-concepts.md
621621
displayName: Excel, Custom Functions
622+
- name: Work with formula value preview
623+
href: excel/custom-functions-formula-value-preview.md
624+
displayName: Excel, Custom Functions
622625
- name: Batch custom function remote calls
623626
href: excel/custom-functions-batching.md
624627
displayName: Excel, Custom Functions

0 commit comments

Comments
 (0)