Skip to main content

Notifications

Community site session details

Community site session details

Session Id : JLqaFqLQ/ObfV3VYJNXGy2
Power Automate - General Discussion
Answered

Excel script error - getWorksheet() throws string only error when inputting an index

Like (0) ShareShare
ReportReport
Posted on 5 Nov 2020 16:09:33 by 13

Hello everyone,

 

Trying to create a flow that will rename the first worksheet of any excel file that is dropped into a sharepoint folder.

 

I had Excel Script record my actions which created the following code:

 

function main(workbook: ExcelScript.Workbook) {

let name_of_worksheet = workbook.getWorksheet("name_of_sheet");
// Rename worksheet to "new_name_of_sheet"
name_of_worksheet.setName("new_name_of_sheet");

}

 

I need the code to be dynamic (it will always be the first sheet but not always have the same name (colleagues working with different languages settings will do that))

 

However when I replace "name_of_sheet" by the index (i.e. workbook.getWorksheet(1) I get the following error message:


[2, 36] Argument of type '1' is not assignable to parameter of type 'string'.

 

How can I get around this? How can I set my Excel Script so it will always rename the first worksheet in excel file that are dropped in a sharepoint?

  • Verified answer
    bBI Profile Picture
    13 on 01 Dec 2021 at 12:53:04
    Re: Excel script error - getWorksheet() throws string only error when inputting an index

    The problem above was because I thought Power BI needs the sheet names to all be the same.

     

    However you can just connect to one file and then in the Transform File section replace the sheet selecting step from name reference to index reference, e.g.:

     

    =Source{[Item="Page1",Kind="Sheet"]}[Data]

    =Source{0}[Data]

    Step-by-step explanation

  • Yutao Huang Profile Picture
    Microsoft Employee on 12 Jun 2021 at 23:23:32
    Re: Excel script error - getWorksheet() throws string only error when inputting an index

    @bBI - Not sure if you have found your answer yet... There are two ways to reference the first worksheet in your workbook:

    let firstSheet = workbook.getFirstWorksheet();

    or

    let firstSheet = workbook.getWorksheets()[0];

     

    Then you can use the firstSheet to do whatever your want (renaming, etc.).

     

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Automate - General Discussion

#1
rpersad Profile Picture

rpersad 16

#2
David_MA Profile Picture

David_MA 10 Super User 2025 Season 1

#3
DBO_DV Profile Picture

DBO_DV 9 Super User 2025 Season 1

Overall leaderboard
Loading started