You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/develop/web-client-performance.md
+35-12Lines changed: 35 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,16 @@
1
1
---
2
2
title: Improve the performance of your Office Scripts
3
3
description: Create faster scripts by understanding the communication between the Excel workbook and your script.
4
-
ms.date: 10/01/2022
4
+
ms.date: 04/10/2024
5
5
ms.localizationpriority: medium
6
6
---
7
7
8
8
# Improve the performance of your Office Scripts
9
9
10
10
The purpose of Office Scripts is to automate commonly performed series of tasks to save you time. A slow script can feel like it doesn't speed up your workflow. Most of the time, your script will be perfectly fine and run as expected. However, there are a few, avoidable scenarios that can affect performance.
11
11
12
+
## Reduce the number of read or write calls
13
+
12
14
The most common reason for a slow script is excessive communication with the workbook. This is particularly noticeable when using Excel on the web. At certain times, your script synchronizes its local data with that of the workbook. This means that any write operations (such as `workbook.addWorksheet()`) are only applied to the workbook when this behind-the-scenes synchronization happens. Likewise, any read operations (such as `myRange.getValues()`) only get data from the workbook for the script at those times. In either case, the script fetches information before it acts on the data. For example, the following code will accurately log the number of rows in the used range.
13
15
14
16
```TypeScript
@@ -21,15 +23,7 @@ console.log(rowCount);
21
23
22
24
Office Scripts APIs ensure any data in the workbook or script is accurate and up-to-date when necessary. You don't need to worry about these synchronizations for your script to run correctly. However, an awareness of this script-to-cloud communication can help you avoid unneeded network calls.
23
25
24
-
## Performance optimizations
25
-
26
-
You can apply simple techniques to help reduce the communication to the cloud. The following patterns help speed up your scripts.
27
-
28
-
- Read workbook data once instead of repeatedly in a loop.
29
-
- Remove unnecessary `console.log` statements.
30
-
- Avoid using try/catch blocks.
31
-
32
-
### Read workbook data outside of a loop
26
+
## Read workbook data outside of a loop
33
27
34
28
Any method that gets data from the workbook can trigger a network call. Rather than repeatedly making the same call, you should save data locally whenever possible. This is especially true when dealing with loops.
35
29
@@ -67,7 +61,7 @@ function main(workbook: ExcelScript.Workbook) {
67
61
> [!NOTE]
68
62
> As an experiment, try replacing `usedRangeValues` in the loop with `usedRange.getValues()`. You may notice the script takes considerably longer to run when dealing with large ranges.
69
63
70
-
###Avoid using `try...catch` blocks in or surrounding loops
64
+
## Avoid using `try...catch` blocks in or surrounding loops
71
65
72
66
We don't recommend using [`try...catch`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/try...catch) statements either in loops or surrounding loops. This is for the same reason you should avoid reading data in a loop: each iteration forces the script to synchronize with the workbook to make sure no error has been thrown. Most errors can be avoided by checking objects returned from the workbook. For example, the following script checks that the table returned by the workbook exists before trying to add a row.
73
67
@@ -89,10 +83,39 @@ function main(workbook: ExcelScript.Workbook) {
89
83
}
90
84
```
91
85
92
-
###Remove unnecessary `console.log` statements
86
+
## Remove unnecessary `console.log` statements
93
87
94
88
Console logging is a vital tool for [debugging your scripts](../testing/troubleshooting.md). However, it does force the script to synchronize with the workbook to ensure the logged information is up-to-date. Consider removing unnecessary logging statements (such as those used for testing) before sharing your script. This typically won't cause a noticeable performance issue, unless the `console.log()` statement is in a loop.
95
89
90
+
## Pause calculations while the scripts runs
91
+
92
+
If your script changes a lot of values, it may trigger excessive recalculations. Control the Excel calculation engine by setting the calculation mode to "manual" while your script runs. Use [`Application.setCalculation`](/javascript/api/office-scripts/excelscript/excelscript.application#excelscript-excelscript-application-setcalculationmode-member(1)) to switch Excel to manually recalculate formulas. Be sure to return the workbook to the original calculation mode when finished.
93
+
94
+
The following sample shows how to change the calculation mode. It also demonstrates how to manually recalculate the workbook with [`Application.calculate`](/javascript/api/office-scripts/excelscript/excelscript.application#excelscript-excelscript-application-calculate-member(1)).
95
+
96
+
```typescript
97
+
/**
98
+
* This script adjusts the calculation mode of the workbook and makes a manual recalculation.
99
+
* Wrap the CalculationMode changes around code that repeatedly updates values.
100
+
*/
101
+
function main(workbook:ExcelScript.Workbook) {
102
+
const application =workbook.getApplication();
103
+
104
+
// Turn off automatic calculations during the script.
As the Office Scripts platform expands to work with [Power Automate](https://make.powerautomate.com/), [Adaptive Cards](/adaptive-cards), and other cross-product features, the details of the script-workbook communication become more intricate. If you need help making your script run faster, please reach out through [Microsoft Q&A](/answers/topics/office-scripts-excel-dev.html). Be sure to tag your question with "office-scripts-dev" so experts can find it and help.
0 commit comments