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?