Office Scripts - Code Review
Tags: powerautomate, powerplatform, officescripts, codereview
Published: 2023-10-09 04:34:23

Office Scripts add much missed functionality to Power Automate (cough regex, cough come on Microsoft). But this adds in 2 areas of complexity that can make your flow runs less stable.

The obvious one is a new moving part and integration. Where ever 2 systems pass data problems arise, and trying to debug both Power Automate and Office Scripts together is exponentially harder than just one of them.

The next is it's a whole different skill set, this is no longer NoCode, or LowCode, this is ProCode. I know some ProCode developers will disagree, but it's TypeScript, just a particular library (admittedly limited). This can mean challenges as the Flow developer isn't trained or experienced in coding. With this is mind code reviews are crucial to ensure that the Office Script is:

- Stable
- Efficient (Each call has response time and size limit)
- Readable (Developer hand over)

---

A starting point before using a Office Script should be is it necessary, following KISS it is best to keep everything in the flow if we can. Office Scripts should only be used for:

- High volume of writes (Power Platform/Excel API throttling)
- High volume of reads (Power Platform API throttling)
- Time Sensitive runs
- Formatting
- No table data
- Missing Power Automate functionality e.g. Office Scripts can be used as a regex matcher, in this case Excel isn't used, only the Office Script.

Once confirmed Office Scripts are the right solution I look at the following:

1. Connector
2. Naming
3. Formatting
4. Error Handling
5. Coding Practices

---

## 1. Connector
Quick and easy one here, the new Run script from SharePoint library is the connector that should be used.

![run script connectors](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zs50cbo8nsb04d71mzgj.png)

Trying to use the OneDrive version is a pain for promoting between environments/accounts (I wrote a full blog with a workaround [here](https://dev.to/wyattdave/fixing-deploying-scripts-in-power-automate-3j7o)), so the new SharePoint version is a must.

## 2. Naming
Naming conventions are key to Office Scripts just like Power Automate, I follow the similar structure:

| Type      | Structure     |
|-----------|---------------|
| Worksheet | wsName        |
| Workbook  | wbName        |
| String    | sVariable     |
| Regex     | regExVariable |
| Range     | rRange        |
| Object    | oVariable     |
| Interface | intStructure  |
| Integer   | iVariable     |
| Float     | fVariable     |
| Boolean   | bVariable     |
| Array     | aVariable     |

With the addition of below when using for loops:

| When   | Variable Name |
|--------|---------------|
| Loop   | i             |
| Column | c             |
| Row    | r             |

When declaring `const `should be used for constants, and `let` for variables (do not use var).

Interfaces should use the int as the structure e.g. intData, with its keys following naming convention. The only exception is when the interface is using Excel columns, in this case colA,colB,colC etc can be used.

![inteface](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pocb5mjo3pn24lxn229o.png)

## 3. Formatting
Code should follow single tab nesting, with comments added where necessary. Unused code should be removed before production.

![code formatting](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/korhzxz3cltidpqf6lqu.png)

## 4. Error Handling
Simple scripts can normally get away without error handling, as this can be caught in the flow. For anything above simple (and if you plan to use outside of Power Automate) a Try Catch should be used. 

![try catch code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/urob4jyq6lky7h6leplj.png)

## 5. Coding Practices
This bucket catches anything that doesn't fall into above categories, for me they are:

**All variables and input parameters are used**
Easy one, if it's not needed remove any variables or inputs of the script.

**Remove all console logs**
They add no value in Power Automate calls and can impact performance (especially in loops)

**Do not use copy / paste** 
Excel based actions like this have big impact on performance, so avoid copy and paste and set one range to another range

**Do not loop over rows**
Any interaction with the file has big impacts on the script performance, instead get range into variable and loop over array.

**Do not write by row** 
Same as above, we don't want to be interacting with Excel, especially in loops, use set range to array.

![set range](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dclrl87lpln02apzcorn.png)

**Use functions for reusable code**
Just like all ProCode, Office Scripts can have reusable functions that are called anywhere within your main script.

![function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s7dkvfefeuhi8hsjc8xf.png)

**Use getRangeBetweenHeaderAndTotal().delete(0) to delete rows**
Still not 100 sure why this is more efficient but use it instead of selectedSheet.getRange("4:16").delete(ExcelScript.DeleteShiftDirection.up)
or 
selectedSheet.getRange("A4:D16).clear(ExcelScript.ClearApplyTo.contents)

![delete rows](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/psokagnxh37q50v9p7q3.png)

---

**Further Reading**
- [Master Office Scripts for Power Automate](https://dev.to/wyattdave/how-to-master-power-automate-scripts-4o8j)
- [Master Office Scripts for Excel](https://dev.to/wyattdave/how-to-master-office-scripts-4ecm)
- [5 Scripts for Power Automate](https://dev.to/wyattdave/5-scripts-every-power-automate-developer-should-know-nep)