Skip to main content

Notifications

Community site session details

Community site session details

Session Id : /qjOx5dcHzN93pDvmhgzLC
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,799 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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

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

#1
WarrenBelz Profile Picture

WarrenBelz 146,745 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 66,091 Most Valuable Professional

Leaderboard
Loading started