web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Office script: remove ...
Power Automate
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?

 

Categories:
  • Verified answer
    lbendlin Profile Picture
    8,726 Super User 2026 Season 1 on at

    here 

    existingTable?.convertToRange();

     

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

  • C4L84 Profile Picture
    27 on at

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

     

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

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Automate

#1
Mohsin Ali Profile Picture

Mohsin Ali 414

#2
11manish Profile Picture

11manish 335 Super User 2026 Season 2

#3
M Bilal Khan Profile Picture

M Bilal Khan 274

Last 30 days Overall leaderboard