Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Automate - Building Flows
Answered

Office script: remove table if exists

(0) ShareShare
ReportReport
Posted on by 27

Hi community, I'm new to power automate and office script - I'm hoping you can help

 

I am writing an office script to create a table in excel. When testing it in power automate it fell over because there was already a table in the file. To get round this I would like to check if there is a table and if so convert it to a range.

 

Here is my script so far:

 

function main(workbook: ExcelScript.Workbook) {

 

  // Get the current, active worksheet.
  let currentWorksheet = workbook.getActiveWorksheet();

 

  //If there is an existing table then remove it
  let existingTable = currentWorksheet.getTables()[0];

 

  existingTable.convertToRange();

 

  // Get the range containing all the cells with data or formatting.
  let usedRange = currentWorksheet.getUsedRange();

 

  // Log the range's address to the console.
  console.log(usedRange.getAddress());

 

  let newTable = workbook.addTable(usedRange,true);

 

  // Rename new table to "Table"
  newTable.setName("Table");
}

 

It works if there is an existing table but doesn't when there isn't one present. 

 

I found this where it it says it is possible to verify if an object is present but I do not know where to place the ? operator.

 

Can you help please?

 

  • C4L84 Profile Picture
    27 on at
    Re: Office script: remove table if exists

    This works - I was dropping the period rather than keeping it. 

     

    Thank you for taking the time to reply, I appreciate it.

  • Verified answer
    lbendlin Profile Picture
    8,041 Super User 2025 Season 1 on at
    Re: Office script: remove table if exists

    here 

    existingTable?.convertToRange();

     

    Note that the best practices article you linked to also mentions you should use absolute sheet addressing.

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