LowCodeStacks™
Sign in

Excel Run script fails in Power Automate: BadGateway and other errors

Run script returns BadGateway when the workbook is read-only or saved as Strict Open XML. Fix both, then work through the other Run script errors Microsoft lists.

Power AutomateFix a problem9 min read

Checked against Microsoft Learn · 8 sourcesPosted by the Maker DeskHow we write guides

Your flow's Run script action, from the Excel Online (Business) connector, fails with 502 and the message BadGateway. The same Office Script runs fine when you open the workbook in Excel and run it yourself. Microsoft documents two causes for this, and neither is in your script's code: the workbook is saved in the ISO strict version of the Excel file format, or the workbook is in read-only mode. Check both first. If neither fits, the rest of this guide matches the other errors Run script gives in Power Automate to Microsoft's documented limits and fixes.

What does the failed action say?

Read the message, then go to its fix
  1. Step 1BadGateway: re-save as Excel Workbook and turn off read-only
  2. Step 2fetch is not defined: move the call into a flow action
  3. Step 3Payload size exceeded: use smaller ranges
  4. Step 4Timed out: split the work into several script calls
  5. Step 5Succeeded but unchanged: refresh methods do nothing in a flow
  6. Step 6Fails after many runs: the per-user daily and per-10-second limits

Fix a BadGateway from Run script

Microsoft's pages name two causes of a BadGateway from this connector. Both are about the workbook file, so the same script can work in Excel and fail in a flow. Neither page says these are the only causes, so if neither fits, see if it's neither below.

Is the workbook saved as Strict Open XML?

Excel can save an .xlsx file in two formats: the default Excel Workbook, and Strict Open XML Spreadsheet, which is an ISO strict version of the same format. Both use the .xlsx extension, so the file name won't tell you which one you have.

The Run script action doesn't support the strict format. Microsoft says flows that run a script against such a workbook return a BadGateway error, and gives coauthoring restrictions as the reason.

Fix:

  1. Open the workbook in the Excel desktop app.
  2. Save it again in the standard Excel Workbook format, not Strict Open XML Spreadsheet. Keep the same name and location, so the flow still finds it.
  3. If you saved a copy under a new name instead, pick the new file in the Run script action.
  4. Run the flow again.

Is the workbook read-only?

The Excel Online (Business) connector's page lists 502 BadGateway as the error you get when the spreadsheet is in read-only mode. Every action in the connector needs write access to the file; without it, Microsoft says, you get 403 Forbidden instead.

Fix: remove the file's read-only state, then check that the account behind the flow's Excel connection can edit the file where it's stored, in OneDrive for Business or SharePoint.

If it's neither

Microsoft's Run script pages don't name a third cause. Our guide to 502 Bad Gateway and other random failures covers general 502s, retries and what to send support.

Also open the failed action's outputs and read the inner error. Microsoft documents a related case for flows called from Power Apps: a 504 whose message says BadGateway, with an inner code of ResponseTimeout, because a synchronous call ran past 120 seconds. Learn doesn't document that pattern for Run script. But if your inner error names a timeout, go to the timeout row in the table below.

Other Run script errors and their fixes

These come from Microsoft's Office Scripts and Excel connector pages:

What you seeWhat it meansFix
The response payload size has exceeded the limit.A request or response went over 5 MB, or a range went over five million cellsRead or write several smaller ranges. Range.getSpecialCells can target just the cells you need
Runtime error: Line X: fetch is not definedA script run by a flow can't call external servicesMake the call with an action in the flow instead. Microsoft points to the HTTP with Azure AD connector or an equivalent action. Pass the result into the script as a parameter
TextEncoder is not defined (or another name)That web API isn't available when a flow runs the script. Microsoft names TextEncoder and Crypto as examplesRewrite that part without it, or do that step in the flow
An error from getActiveCell, getSelectedRange, getSelectedRanges, getActiveChart, getActiveSlicer, Range.select or Chart.activateThese depend on what a person has selected, and in a flow there's no personName the worksheet and range in the script instead; see the example below
The action times outSynchronous operations in Power Automate have a 120-second timeoutMake the script faster, or split the work into several Run script calls
429 Too many requestsRun script allows up to 3 calls in 10 seconds, and more are throttledAdd a delay around the action, for example inside a loop
Run script calls fail after many runs in one dayEach user can make 1,600 Run script calls a day; the count resets at 12:00 AM UTCDo more work in each call, so the flow makes fewer of them
A call that passes a lot of data in failsThe parameters passed to Run script can be at most 30,000,000 bytes (28.6 MB)Send less data in one call
403 ForbiddenThe connection's account can't write to the fileGive that account edit access
Due to organizational policies, you can’t access this resource from this untrusted device. or, on older Excel versions, We can't find this script. It may have been deleted by another user.A Conditional Access policy blocks the script. Microsoft says these appear in Power Automate when the flow is run from an unmanaged deviceAsk your admin about the policy, and run the flow from a device your organisation manages

Three more limits from the connector's page are worth knowing:

  • The connector supports Excel files up to 25 MB.
  • A file can stay locked for an update or delete for up to 6 minutes after the connector last used it.
  • People, other connectors and flows editing the same file at the same time aren't supported, and can cause conflicts.

The run succeeded but nothing changed

The run is green, but the workbook didn't update. Microsoft documents this: most refresh methods do nothing when a flow runs the script, and they still report success.

  • PivotTable.refresh, Workbook.refreshAllPivotTables and Worksheet.refreshAllPivotTables do nothing in a flow.
  • Workbook.refreshAllDataConnections refreshes only Power BI sources. For any other source, it does nothing.
  • Power Automate doesn't refresh formulas that use links to other workbooks.

Two other behaviours can put data in the wrong place without an error:

  • Workbook.getActiveWorksheet returns the first worksheet in a flow, or the one your script activated with Worksheet.activate, not the sheet someone last looked at.
  • Power Automate always works in UTC, while Excel uses each person's local time zone. A script that uses dates can give different results in a flow.

Use absolute references for worksheets and ranges. Microsoft says this keeps the flow consistent, even if worksheets are rearranged. This example follows Microsoft's guidance; the sheet and cell names are made up:

typescript
function main(workbook: ExcelScript.Workbook) {
  // Name the sheet: a flow has no "active" sheet or selection.
  const sheet = workbook.getWorksheet("Orders");
  if (!sheet) {
    throw new Error("The workbook has no sheet named Orders.");
  }
  sheet.getRange("B2").setValue("Updated by the flow");

  // Fails in a flow, because there is no selected cell:
  // workbook.getActiveCell().setValue("Updated by the flow");
}

When the action itself looks wrong

  • Parameters or outputs are missing. The Run script action keeps a copy of the script's parameters and return value from when you added it. If you've changed them since, delete the action and add it again. Also check that the script only uses the parameter and return types Microsoft supports for flows.
  • Choosing the workbook. Microsoft says to pick the workbook with the action's file browser rather than typing its name.
  • The script expects a whole array. Array fields take single items by default. For a script that takes the entire array, select Switch to input entire array, the button in the top-right corner of the field.
  • The script lives in SharePoint. Run script only finds scripts in your OneDrive's default location. For a script saved in a SharePoint library, use Run script from SharePoint library.

Warning

Running Office Scripts from Power Automate needs a business licence of Microsoft 365. The Run script action isn't available in sovereign clouds such as GCC, GCC High and DoD.

Do

  • Save workbooks that flows use in the standard Excel Workbook format.
  • Name worksheets and ranges in the script; never rely on a selection.
  • Make external calls in the flow, and pass the data into the script.

Don't

  • Don't rebuild the script for a BadGateway before checking the file's format and read-only state.
  • Don't count on a script to refresh pivot tables or data connections in a flow.
  • Don't loop over Run script faster than 3 calls in 10 seconds.

If your flow reads the table with List rows present in a table and gets only part of it, see why List rows returns only 256 rows.

Sources

Add LowCodeStacks as a preferred source on Google

Did this fix it?

Comments

No comments yet. Share what worked for you.

Sign in to comment. Comments show under a display name you choose, never your email.