Checking if an item does not always for...

Jonathan Martin 6 Reputation points
2022-07-01T20:14:13.067+00:00

According to https://learn.microsoft.com/en-us/office/dev/scripts/develop/scripting-fundamentals#verify-an-object-exists-in-the-collection
one can check if an object already exists and delete before trying to create a replacement;
// Get the table named "MyTable".
let myTable = workbook.getTable("MyTable");

// If the table is in the workbook, myTable will have a value.
// Otherwise, the variable will be undefined and go to the else clause.
if (myTable) {
let worksheetName = myTable.getWorksheet().getName();
console.log(MyTable is on the ${worksheetName} worksheet);
} else {
console.log(MyTable is not in the workbook.);
}

But I am finding this is not the case with the Shapes collection. If I use the above method to check if a shape with a name already exists, I get a 'Resource does not exist' error. Instead I can only get the colelction and iterate through it to find a specific shape;

let Allshapes = the_worksheet.getShapes();
Allshapes.forEach((shape) =>
{
if (shape.getName() === "Logo")
{
shape.delete();
}
});

Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,298 questions
{count} votes