Skip to content

Commit da3c437

Browse files
author
github-actions
committed
Merge branch 'main' into live
2 parents 2ae40b9 + 7933ed4 commit da3c437

7 files changed

+164
-2
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
---
2+
description: Tips and best practices for Excel custom functions in your Office Add-ins.
3+
title: Best practices for custom functions in Excel
4+
ms.date: 12/20/2024
5+
ms.topic: overview
6+
ms.custom: scenarios:getting-started
7+
ms.localizationpriority: medium
8+
---
9+
10+
# Best practices for custom functions in Excel
11+
12+
This article includes tips, best practices, and Office Add-ins ecosystem information for new developers of custom functions add-ins.
13+
14+
The following diagram illustrates the interaction between a custom function and the two main components involved in custom function add-ins: Excel and external services.
15+
16+
:::image type="content" source="../images/custom-functions-add-in-components.png" alt-text="The custom functions add-in communicates with both Excel and an external service, but Excel and the external service don't communicate directly with each other.":::
17+
18+
**Excel** allows you to integrate your own custom functions into the application and run them like built-in functions.
19+
20+
The **custom functions add-in** defines the logic for your functions and how they interact with Excel and Office JavaScript APIs. To learn how to create a custom functions add-in, see the [custom functions tutorial](../tutorials/excel-tutorial-create-custom-functions.md).
21+
22+
An **external service** is optional. It can give your add-in capabilities like importing data from outside the workbook. The custom functions add-in specifies how external data is incorporated into the workbook. To learn more, see [Receive and handle data with custom functions](custom-functions-web-reqs.md).
23+
24+
## Optimize custom functions recalculation efficiency
25+
26+
In general, custom functions recalculation follows the established pattern of [recalculation in Excel](/office/client-developer/excel/excel-recalculation). When recalculation is triggered, Excel enters a three-stage process: construct a dependency tree, construct a calculation chain, and then recalculate the cells. To optimize recalculation efficiency in your add-in, consider the level of nesting within your custom functions, the Excel calculation mode, and the limitations of volatile functions.
27+
28+
### Nesting in custom functions
29+
30+
A custom function can accept another custom function as an argument, making the argument a nested custom function. The recalculation of the outer custom function depends on the result of the nested function, leading to increased time consumption with each additional nested function. Minimize the number of nested levels in your custom functions to improve recalculation efficiency. The following code snippets demonstrate two approaches for adding values in the workbook that produce similar outputs. **Option 1** uses an array to call values as a single parameter, while **Option 2** calls each value as a separate parameter, so **Option 1** is more efficient.
31+
32+
#### Option 1: Increase efficiency with limited nesting
33+
34+
> [!NOTE]
35+
> This is the recommended approach. It uses an array to call values as a single parameter and avoid unnecessary nesting, so it's more efficient than **Option 2**.
36+
37+
```js
38+
/**
39+
* Returns the sum of input numbers.
40+
* @customfunction
41+
*/
42+
function Add(args: number[]): number {
43+
let total = 0;
44+
args.forEach(value => {
45+
total += value;
46+
});
47+
48+
return total;
49+
}
50+
```
51+
52+
#### Option 2: More nesting is inefficient
53+
54+
> [!NOTE]
55+
> This approach isn't recommended. **Option 1** and **Option 2** produce similar outputs, but **Option 2** uses more parameters and is less efficient.
56+
57+
```js
58+
/**
59+
* Returns the sum of two numbers.
60+
* @customfunction
61+
*/
62+
function Add(arg1: number, arg2: number): number {
63+
return arg1 + arg2;
64+
}
65+
```
66+
67+
### Excel calculation modes
68+
69+
Excel has three calculation modes: Automatic, Automatic Except Tables, and Manual. To determine which calculation mode best fits your custom function design, refer to the [Calculation Modes, Commands, Selective Recalculation, and Data Table](/office/client-developer/excel/excel-recalculation#calculation-modes-commands-selective-recalculation-and-data-tables#calculation-modes-commands-selective-recalculation-and-data-tables) section in the main [Excel Recalculation](/office/client-developer/excel/excel-recalculation) article.
70+
71+
Set the calculation mode for your add-in with the [Excel.CalculationMode enum](/javascript/api/excel/excel.calculationmode) based on your scenario. Note that `automatic` calculation mode may trigger recalculation often and reduce the efficiency of your add-in.
72+
73+
### Volatile function limitations
74+
75+
Custom functions allow you to create your own volatile functions, similar to the `NOW` and `TODAY` functions in Excel. During recalculation, Excel evaluates cells that contain volatile functions and all of their dependent cells. As a result, using many volatile functions may slow recalculation time so limit the number of volatile functions in your add-in to optimize efficiency. For additional information, see [Volatile and Non-Volatile Functions](/office/client-developer/excel/excel-recalculation#volatile-and-non-volatile-functions).
76+
77+
## Design approaches to improve efficiency
78+
79+
Custom functions add-ins allow for flexible designs, which means that different add-in designs can produce the same output for your end users.
80+
81+
### Multiple results
82+
83+
You can return multiple results from your custom function with multiple functions or with one function.
84+
85+
To return multiple results with one function, use a dynamic array. This is usually the recommended approach because dynamic arrays only require updating a single cell to trigger recalculation for all results.
86+
87+
:::image type="content" source="../images/custom-functions-dynamic-array.png" alt-text="The output of a dynamic array.":::
88+
89+
Keep in mind that using dynamic arrays becomes less efficient the larger your dataset is, because each recalculation processes more data. To learn more about dynamic arrays in custom functions, see [Return multiple results from your custom function](custom-functions-dynamic-arrays.md).
90+
91+
Another way to return multiple results is to use multiple functions and return a single result for each function. A benefit of using multiple functions is that your end user can decide precisely which formula they want to update and then only trigger recalculation for that formula. This is particularly helpful when relying on external services that may respond slowly.
92+
93+
:::image type="content" source="../images/custom-functions-not-dynamic-array.png" alt-text="The output of multiple functions instead of a dynamic array.":::
94+
95+
### Complex data structures
96+
97+
[Data types](custom-functions-data-types-concepts.md) are the best way to handle complex data structures in custom functions add-ins. Data types support [Excel errors](custom-functions-errors.md) and [formatted number values](custom-functions-data-types-concepts.md#output-a-formatted-number-value). Data types also allow for designing [entity value cards](excel-data-types-entity-card.md), extending Excel data beyond the 2-dimensional grid.
98+
99+
## Improve the user experience of calls to external services
100+
101+
Custom functions can fetch data from remote locations beyond the workbook, such as the web or a server. For more information about fetching data from an external service, see [Receive and handle data with custom functions](custom-functions-web-reqs.md). To maintain efficiency when calling external services, consider batching external calls, minimizing roundtrip duration for each call, and including messages in your add-in to communicate delays to your end user.
102+
103+
### Batch custom function remote calls
104+
105+
If your custom functions call a remote service, use a batching pattern to reduce the number of network calls to the remote service. To learn more, see [Batching custom function calls for a remote service](custom-functions-batching.md).
106+
107+
### Minimize roundtrip duration
108+
109+
Remote service connections can have a large impact on custom function performance. To reduce this impact, use these strategies:
110+
111+
- Server-heavy processing should be handled efficiently in the remote server to shorten the end-to-end latency for a custom function. For example, have parallel computing designed on the server. If your service is deployed on Azure, consider optimization using [high-performance computing on Azure](/azure/architecture/topics/high-performance-computing).
112+
- Reduce the number of service calls by optimizing the add-in flow. For example, only send necessary calls to a remote service.
113+
114+
### Improve user-perceived performance through add-in user experience (UX)
115+
116+
While a custom function is calling an external service, the cell with the custom function displays the **#BUSY!** error. If a delay while calling an external service is inevitable, consider providing messages through the add-in task pane to explain the delay to your end users. This information helps manage their expectations. The following image shows an example.
117+
118+
:::image type="content" source="../images/custom-functions-delay-example.png" alt-text="The delay message says 'There may be a delay. We're getting the data ready for you'.":::
119+
120+
For more information about how to share data between a custom function and a task pane, see [Share data and events between Excel custom functions and the task pane](../tutorials/share-data-and-events-between-custom-functions-and-the-task-pane-tutorial.md).
121+
122+
To display a message in the add-in task pane that notifies users of a delay, make the following changes after ensuring that your add-in uses a shared runtime.
123+
124+
1. In **taskpane.js** add a function that calls the notification.
125+
126+
```js
127+
export function showNotification(message){
128+
const label = document.getElementById("item-subject");
129+
label.innerHTML = message;
130+
}
131+
```
132+
133+
1. In **function.js**, import the `showNotification` function.
134+
135+
```js
136+
export function showNotification(message){
137+
const label = document.getElementById("item-subject");
138+
label.innerHTML = message;
139+
}
140+
```
141+
142+
1. In **function.js**, call `showNotification` when running the calculation that may include a delay.
143+
144+
```js
145+
export async function longCalculation(param) {
146+
await Office.addin.showAsTaskpane();
147+
showNotification("It may take some time as we prepare the data.");
148+
// Perform long operation
149+
// ...
150+
// ...
151+
return answer;
152+
}
153+
```
154+
155+
## See also
156+
157+
- [Receive and handle data with custom functions](custom-functions-web-reqs.md)
158+
- [Batch custom function calls for a remote service](custom-functions-batching.md)
159+
- [Create custom functions in Excel tutorial](../tutorials/excel-tutorial-create-custom-functions.md)
12.9 KB
Loading
66.7 KB
Loading
37.6 KB
Loading
57.7 KB
Loading

docs/outlook/faq-nested-app-auth-outlook-legacy-tokens.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ The general availability (GA) date for NAA depends on which channel you are usin
4040

4141
### Are COM Add-ins affected by the deprecation of legacy Exchange Online tokens?
4242

43-
It's very unlikely any COM add-ins are affected by the deprecation of legacy Exchange Online tokens. Outlook web add-ins are primarily affected because they can use Office.js APIs that rely on Exchange tokens. For more information, see[How do i know if my outlook add in relies on legacy tokens](#how-do-i-know-if-my-outlook-add-in-relies-on-legacy-tokens). The Exchange tokens are used to access Exchange Web Services (EWS) or Outlook REST APIs, both of which are also deprecated. If you suspect a COM add-in might be affected, you can test it by using it on a tenant with Exchange tokens turned off. For more information, see [Turn legacy Exchange Online tokens on or off](turn-exchange-tokens-on-off.md).
43+
It's very unlikely any COM add-ins are affected by the deprecation of legacy Exchange Online tokens. Outlook web add-ins are primarily affected because they can use Office.js APIs that rely on Exchange tokens. For more information, see [How do i know if my outlook add in relies on legacy tokens](#how-do-i-know-if-my-outlook-add-in-relies-on-legacy-tokens). The Exchange tokens are used to access Exchange Web Services (EWS) or Outlook REST APIs, both of which are also deprecated. If you suspect a COM add-in might be affected, you can test it by using it on a tenant with Exchange tokens turned off. For more information, see [Turn legacy Exchange Online tokens on or off](turn-exchange-tokens-on-off.md).
4444

4545
## Microsoft 365 administrator questions
4646

@@ -229,4 +229,4 @@ It's very important that you always request an access token to your own services
229229
## Related content
230230

231231
- [Enable SSO in an Office Add-in using nested app authentication](../develop/enable-nested-app-authentication-in-your-add-in.md).
232-
- [Outlook add-in with SSO using nested app authentication](https://github.com/OfficeDev/Office-Add-in-samples/tree/main/Samples/auth/Outlook-Add-in-SSO-NAA).
232+
- [Outlook add-in with SSO using nested app authentication](https://github.com/OfficeDev/Office-Add-in-samples/tree/main/Samples/auth/Outlook-Add-in-SSO-NAA).

docs/toc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,9 @@ items:
580580
- name: Share custom function data with the task pane
581581
href: tutorials/share-data-and-events-between-custom-functions-and-the-task-pane-tutorial.md
582582
displayName: Excel, Custom Functions
583+
- name: Best practices
584+
href: excel/custom-functions-get-started.md
585+
displayName: Excel, Custom Functions
583586
- name: Naming and localization
584587
href: excel/custom-functions-naming.md
585588
displayName: Excel, Custom Functions

0 commit comments

Comments
 (0)