cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
ikmalfikri
Helper I
Helper I

Power Autoamate BUG/PROBLEM with Office Script

Hi,

 

I need help with Power Automate with Office Script/Excel Script.
So..I want to run a script to automatically create a table but power automate have problem with things such as GetSurroundingRegion(),getRowCount(),getValues(),

The script works perfectly fine if i run it manually but it all become problem when Power Automate runs it.

 

There's Create Table connector in Power Automate but how do i want to let the table height be dynamic as the function only alllows for constant value of row.

1 ACCEPTED SOLUTION

Accepted Solutions
DamoBird365
Super User
Super User

Hi @ikmalfikri 

 

I've recently written the following which will build a table starting from A9 and finishing at L (last active row number).

 

You might be able to work with this?

 

function main(workbook: ExcelScript.Workbook) {
  
  // Get the first worksheet 
  const sheet = workbook.getFirstWorksheet();
  
  //get active range of WorkSheet
  let range = workbook.getActiveWorksheet().getUsedRange();

  // Get last used row of WorkSheet
  let lastrow = range.getRowCount();

  //get table range  
  const TableRange = `A9:L${lastrow}`;

  // Create a table using the data range.
  let newTable = workbook.addTable(sheet.getRange(TableRange), true);
  newTable.setName("TestTable");
  
  // Get the first (and only) table in the worksheet.
  let table = sheet.getTables()[0];

  // Get the data from the table.
  let tableValues = table.getRangeBetweenHeaderAndTotal().getValues();

  console.log(lastrow);
  return tableValues

}

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Cheers,
Damien


P.S. take a look at my new blog here and like & subscribe to my YouTube Channel thanks 😉

View solution in original post

2 REPLIES 2
DamoBird365
Super User
Super User

Hi @ikmalfikri 

 

I've recently written the following which will build a table starting from A9 and finishing at L (last active row number).

 

You might be able to work with this?

 

function main(workbook: ExcelScript.Workbook) {
  
  // Get the first worksheet 
  const sheet = workbook.getFirstWorksheet();
  
  //get active range of WorkSheet
  let range = workbook.getActiveWorksheet().getUsedRange();

  // Get last used row of WorkSheet
  let lastrow = range.getRowCount();

  //get table range  
  const TableRange = `A9:L${lastrow}`;

  // Create a table using the data range.
  let newTable = workbook.addTable(sheet.getRange(TableRange), true);
  newTable.setName("TestTable");
  
  // Get the first (and only) table in the worksheet.
  let table = sheet.getTables()[0];

  // Get the data from the table.
  let tableValues = table.getRangeBetweenHeaderAndTotal().getValues();

  console.log(lastrow);
  return tableValues

}

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Cheers,
Damien


P.S. take a look at my new blog here and like & subscribe to my YouTube Channel thanks 😉

thank you very much..finally it works..

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

May UG Leader Call Carousel 768x460.png

June User Group Leader Call

Join us on June 28 for our monthly User Group leader call!

MPA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

Learn to digitize and optimize business processes and connect all your applications to share data in real time.

Power Automate Designer Feedback_carousel.jpg

Help make Flow Design easier

Are you new to designing flows? What is your biggest struggle with Power Automate Designer? Help us make it more user friendly!

Users online (2,171)