Skip to main content

Notifications

Community site session details

Community site session details

Session Id : CW12aMBLI8IESux5yHB6Hm
Power Automate - Building Flows
Answered

Office script: remove table if exists

Like (0) ShareShare
ReportReport
Posted on 11 Jan 2023 12:29:36 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 16 Jan 2023 at 09:42:10
    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
    7,906 Super User 2025 Season 1 on 14 Jan 2023 at 23:19:41
    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

Announcing the Engage with the Community forum!

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

🌸 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…

Leaderboard > Power Automate - Building Flows

#1
David_MA Profile Picture

David_MA 222 Super User 2025 Season 1

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 215 Super User 2025 Season 1

#3
stampcoin Profile Picture

stampcoin 190

Overall leaderboard
Loading started