Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Automate - Building Flows
Answered

Excel script fails probably because of formulas

(0) ShareShare
ReportReport
Posted on by 5

I'm trying to automate a new workbook each 1st working day of the month which create a table with all the working days of that month.

Using the record functionality I was able to create the script and the script is working when I run the script in Excel (desktop and online).

 

However, when I try to create a power automate flow using this script I get an error:

"We were unable to run the script. Please try again.
Office JS error: Line 4: Range setFormulasLocal: The argument is invalid or missing or has an incorrect format."

 

Script:

function main(workbook: ExcelScript.Workbook) {
  let selectedSheet = workbook.getActiveWorksheet();
  // Set range A1:B3 on selectedSheet
  selectedSheet.getRange("A1:B3").setFormulasLocal([["Week","Date"],["=WEEKDAY(B2)","=WORKDAY(EOMONTH(TODAY();-1);1)"],[null,"=WORKDAY($B$2;SEQUENCE(NETWORKDAYS($B$2;EOMONTH($B$2;0))-1))"]]);
  // Set format for range B:B on selectedSheet
  selectedSheet.getRange("B:B").setNumberFormatLocal("d-m-jjjj");
  // Auto fill range
  selectedSheet.getRange("A2").autoFill();
  // Set range A2 on selectedSheet
  selectedSheet.getRange("A2").setFormulaLocal("=WEEKNUM(B2)");
  // Auto fill range
  selectedSheet.getRange("A2").autoFill();
 
It looks like the way the formula was used in the script is not working in the power automate flow.
Anyone has more experience with this or have an alternative?
  • Verified answer
    rzaneti Profile Picture
    4,096 Super User 2025 Season 1 on at
    Re: Excel script fails probably because of formulas

    Hi @QueNgo ,

     

    It looks like a Script error, so it ideally should not be working neither in Power Automate, nor in Excel directly by suing the "Automate" Pane. 

     

    According to the error message, the problem is in line 4, which is the following:

    selectedSheet.getRange("A1:B3").setFormulasLocal([["Week","Date"],["=WEEKDAY(B2)","=WORKDAY(EOMONTH(TODAY();-1);1)"],[null,"=WORKDAY($B$2;SEQUENCE(NETWORKDAYS($B$2;EOMONTH($B$2;0))-1))"]]);

     

    I ran some tests here and it looks like your error is caused by the semicolons in the formulas. I recommend you to change them for commas directly in the script, save it, and try to run it again. 

     

    Also, for further reference about how to start to work with Office Scripts (sometimes it can be tricky!), I recommend you to check this blog post: http://digitalmill.net/2023/06/19/get-started-with-office-scripts/ 

     

    Let me know if it works for you or if you need any additional help!

     

    -------------------------------------------------------------------------
    If this is the answer for your question, please mark the post as Solved.
    If this answer helps you in any way, please give it a like.

    http://digitalmill.net/ 
    https://www.linkedin.com/in/raphael-haus-zaneti/ 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Paul Stork – Community Spotlight

We are honored to recognize Paul Stork as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 497 Super User 2025 Season 1

#2
David_MA Profile Picture

David_MA 436 Super User 2025 Season 1

#3
Riyaz_riz11 Profile Picture

Riyaz_riz11 244 Super User 2025 Season 1