|
1 | 1 | --- |
2 | | -ms.date: 09/09/2022 |
| 2 | +ms.date: 10/22/2025 |
3 | 3 | description: Batch custom functions together to reduce network calls to a remote service. |
4 | | -title: Batching custom function calls for a remote service |
| 4 | +title: Batch custom function calls for a remote service |
5 | 5 | ms.topic: best-practice |
6 | 6 | ms.localizationpriority: medium |
7 | 7 | --- |
8 | 8 |
|
9 | 9 | # Batch custom function calls for a remote service |
10 | 10 |
|
11 | | -If your custom functions call a remote service you can use a batching pattern to reduce the number of network calls to the remote service. To reduce network round trips you batch all the calls into a single call to the web service. This is ideal when the spreadsheet is recalculated. |
| 11 | +Use batching to group calls to a remote service into a single network request. This reduces the number of network round trips to your remote service and helps your worksheet finish recalculating faster. |
12 | 12 |
|
13 | | -For example, if someone used your custom function in 100 cells in a spreadsheet, and then recalculated the spreadsheet, your custom function would run 100 times and make 100 network calls. By using a batching pattern, the calls can be combined to make all 100 calculations in a single network call. |
| 13 | +Here's a batching example scenario: If 100 cells call the custom function, send one request that lists all 100 operations. The service returns 100 results in a single response. |
14 | 14 |
|
15 | | -[!include[Excel custom functions note](../includes/excel-custom-functions-note.md)] |
16 | | - |
17 | | -## View the completed sample |
18 | | - |
19 | | -To view the completed sample, follow this article and paste the code examples into your own project. For example, to create a new custom function project for TypeScript use the [Yeoman generator for Office Add-ins](../develop/yeoman-generator-overview.md), then add all the code from this article to the project. Run the code and try it out. |
20 | | - |
21 | | -Alternatively, download or view the complete sample project at [Custom function batching pattern](https://github.com/OfficeDev/Office-Add-in-samples/tree/main/Excel-custom-functions/Batching). If you want to view the code in whole before reading any further, take a look at the [script file](https://github.com/OfficeDev/Office-Add-in-samples/blob/main/Excel-custom-functions/Batching/src/functions/functions.js). |
22 | | - |
23 | | -## Create the batching pattern in this article |
| 15 | +## Key points |
24 | 16 |
|
25 | 17 | To set up batching for your custom functions you'll need to write three main sections of code. |
26 | 18 |
|
27 | 19 | 1. A [push operation](#add-the-_pushoperation-function) to add a new operation to the batch of calls each time Excel calls your custom function. |
28 | 20 | 2. A [function to make the remote request](#make-the-remote-request) when the batch is ready. |
29 | 21 | 3. [Server code to respond to the batch request](#process-the-batch-call-on-the-remote-service), calculate all of the operation results, and return the values. |
30 | 22 |
|
| 23 | +[!include[Excel custom functions note](../includes/excel-custom-functions-note.md)] |
| 24 | + |
| 25 | +## Create the batching pattern in this article |
| 26 | + |
31 | 27 | In the following sections, you'll learn how to construct the code one example at a time. It's recommended you create a brand-new custom functions project using the [Yeoman generator for Office Add-ins](../develop/yeoman-generator-overview.md) generator. To create a new project, see [Get started developing Excel custom functions](../quickstarts/excel-custom-functions-quickstart.md). You can use TypeScript or JavaScript. |
32 | 28 |
|
| 29 | +> [!TIP] |
| 30 | +> To view the completed sample, create a new custom functions project with the Yeoman generator for Office Add-ins. Copy and paste the code examples into your project then, run the code and try it out. |
| 31 | +> |
| 32 | +> Alternatively, download or view the complete sample project at [Custom function batching pattern](https://github.com/OfficeDev/Office-Add-in-samples/tree/main/Excel-custom-functions/Batching). If you want to view the entire code before continuing, take a look at the [script file](https://github.com/OfficeDev/Office-Add-in-samples/blob/main/Excel-custom-functions/Batching/src/functions/functions.js). |
| 33 | +
|
33 | 34 | ## Batch each call to your custom function |
34 | 35 |
|
35 | | -Your custom functions work by calling a remote service to perform the operation and calculate the result they need. This provides a way for them to store each requested operation into a batch. Later you'll see how to create a `_pushOperation` function to batch the operations. First, take a look at the following code example to see how to call `_pushOperation` from your custom function. |
| 36 | +Your custom functions work by calling a remote service to perform operations and return results. This provides a way for them to store each requested operation into a batch. Later you'll see how to create a `_pushOperation` function to batch the operations. First, review the following code example to see how to call `_pushOperation` from your custom function. |
36 | 37 |
|
37 | 38 | In the following code, the custom function performs division but relies on a remote service to do the actual calculation. It calls `_pushOperation` to batch the operation along with other operations to the remote service. It names the operation **div2**. You can use any naming scheme you want for operations as long as the remote service is also using the same scheme (more on the remote service later). Also, the arguments the remote service will need to run the operation are passed. |
38 | 39 |
|
39 | | -### Add the div2 custom function |
| 40 | +### Add the `div2` custom function |
40 | 41 |
|
41 | 42 | Add the following code to your **functions.js** or **functions.ts** file (depending on if you used JavaScript or TypeScript). |
42 | 43 |
|
@@ -176,7 +177,7 @@ Add the following code to your **functions.js** or **functions.ts** file. |
176 | 177 |
|
177 | 178 | ```javascript |
178 | 179 | // This function simulates the work of a remote service. Because each service |
179 | | -// differs, you will need to modify this function appropriately to work with the service you are using. |
| 180 | +// differs, modify this function as needed to work with the service you are using. |
180 | 181 | // This function takes a batch of argument sets and returns a promise that may contain a batch of values. |
181 | 182 | // NOTE: When implementing this function on a server, also apply an appropriate authentication mechanism |
182 | 183 | // to ensure only the correct callers can access it. |
@@ -232,6 +233,18 @@ To modify the `_fetchFromRemoteService` function to run in your live remote serv |
232 | 233 | - Apply an appropriate authentication mechanism. Ensure that only the correct callers can access the function. |
233 | 234 | - Place the code in the remote service. |
234 | 235 |
|
| 236 | +## When to avoid batching |
| 237 | + |
| 238 | +Batching adds a small delay and some extra code. Avoid batching in the following scenarios. |
| 239 | + |
| 240 | +| Scenario | Negative impact of batching | Recommendation | |
| 241 | +|----------|-------------------|----------------| |
| 242 | +| Single or very few calls | Extra wait for timer | Call service directly if list is still empty | |
| 243 | +| Very large input data per call | Request might get too large | Limit size or send those calls alone | |
| 244 | +| Some calls are much slower than others | One slow call delays faster ones | Group slow types separately | |
| 245 | +| Need near-instant result (less than 50 ms) | Timer adds delay | Use a shorter timer or skip batching | |
| 246 | +| Server already combines work | No benefit | Skip batching on the client | |
| 247 | + |
235 | 248 | ## Next steps |
236 | 249 |
|
237 | 250 | Learn about [the various parameters](custom-functions-parameter-options.md) you can use in your custom functions. Or review the basics behind making [a web call through a custom function](custom-functions-web-reqs.md). |
|
0 commit comments