Hi @powerappdummy ,
Yes, this error can happens if you already have a table in your file at the same range. Your table may have a name, so you can access it directly, without necessarily add a new one.
To access your existing table, you can replace your 'let new table..' row for this one:
let newTable = workbook.getTable([TABLE_NAME]);
Inside the parenthesis, you can put the table name, as it appears in your 'Table Design' tab in Excel. If you want to cover the both scenarios (use a table if it exists, and create it if it not exists), you can use the following block of code:
if(workbook.getTable([TABLE_NAME])!== undefined){
let newTable = workbook.getTable([TABLE_NAME])
} else{
let newTable = selectedSheet.addTable(usedRange, true).addName([TABLE_NAME]) //with this additional method, you will add a name for your table when it is created :)
}
Also, if you need to delete all records from your table before use it, you can use the following block of code:
try {
newTable.getRangeBetweenHeaderAndTotal().delete(ExcelScript.DeleteShiftDirection.up)
} catch{
null
}
In this block, you are basically capturing all rows from your table (except for the header and total rows) and deleting it. We are encapsulating it inside a try/catch because sometimes (at least in my use cases) you already have an empty table, so if you just delete it without insert into the "try" it will return an error.
Let me know if it works for you, and if you find any additional issues with this task, do not hesitate to reach me. I know that these JavaScript commands may be difficult when we start to work with, and I will be happy to help.