I am using PowerAutomate to add data to a report each day. The data comes in on email as a CSV attachment, this is parsed with an Azure function into a 2D array and stringified to JSON, this is then passed to an excel script that parses the JSON, truncates the row arrays to the correct width, adds formulas to the row arrays for calculated columns and uses the table.addRows()
function to add the data in to the table.
This happens on three different tables from three different reports that come in (spaced 30 mins apart).
- One is small, adding ~10 rows a day, with no added formula columns, this is always fine and successful in PowerAutomate.
- The second is ~150 rows a day, with one added formula column. This always succeeds in adding the data, but sometimes the PowerAutomate connector fails with 504 BadGateway - Request to Graph API has timed out.
- The third is ~150 rows a day, with a few added formula columns. This usually fails, either successfully adding the date but returning the error above, or by failing to add the data and returning We were unable to run the script. Please try again. Office JS error: Line 44: Table addRows: The request failed with status code of 504, error code UnknownError
Here is the function itself (specifically the third example):
function main(workbook: ExcelScript.Workbook, inputData: string) {
// Get formula columns as array of strings
const additionalData = workbook.getWorksheet(`contact_time`).getRange("W2:AG2").getFormulas()[0]
// Parse inputData to get actual array
const dataToAdd: Array<Array<string>> = JSON.parse(inputData)
// Remove first row's data as is just headers
dataToAdd.shift()
if (dataToAdd.length == 0) {
return
}
// Truncate each input array row and add formula array columns on to the end
for (const row of dataToAdd) {
row.length = 22
for (const column of additionalData) {
row.push(column)
}
}
// Add data to table
workbook.getTable("t").addRows(null, dataToAdd)
}
In order to try and optimise this I have
- in the script, accessed the workbook the minimum number of times possible for fewer requests
- removed the call to
.getFormulas()
and hardcoded an array instead - changed the PowerAutomate timeout to P1D and retries to none
- turned off recalculation for the workbook/set calculation mode to manual (both at workbook level and at the start of the script itself)
This works 100% of the time if I paste in the JSON to the function and run it in the browser. I can't work out if this is due to the actual function timing out, PowerAutomate failing to wait long enough for the result, some timeout in the Graph API, or something else.
The workbook is under 5mb in size, the table that fails most often has about 13.000 rows.
Edit:
The workbook I am adding to has worksheets for these three tables, plus a few more with analysis tables as well. I also tested removing different sheets, references etc. The slowdown seems to be the formula columns that already exist in the table, as only by completely removing these columns was the function successful in every instance. Now instead of timing out in 90 seconds, it completes in 8 seconds without any issues. Why would this affect the speed with recalculation turned off?