Loop through all shapes in sheet using OfficeScripts

To loop through all shapes in a sheet using OfficeScripts, you can utilize the shapes property of the Worksheet object. Here’s an example of a function that loops through all shapes in a sheet:

function loopThroughShapesInSheet() {
  // Get the active worksheet
  var worksheet = context.workbook.worksheets.getActiveWorksheet();

  // Load the shapes property of the worksheet
  var shapes = worksheet.shapes;
  context.load(shapes, "items/name");

  // Execute the request and get the shapes
  return context.sync()
    .then(function () {
      var shapeNames = shapes.items.map(function (shape) {
        return shape.name;
      });

      console.log("Shape Names: " + shapeNames.join(", "));
    })
    .catch(function (error) {
      console.log("Error occurred: " + error);
    });
}

In this example, the loopThroughShapesInSheet function fetches the active worksheet using context.workbook.worksheets.getActiveWorksheet(). The shapes property of the worksheet is then loaded using worksheet.shapes, and the name property of each shape is specified to be loaded.

After executing the request and synchronizing the changes with context.sync(), the function retrieves the names of all shapes by mapping over the items array of the shapes object. The shape names are then logged to the console.

To use this function, you can call it as follows:

loopThroughShapesInSheet() .catch(function (error) { console.log("Error occurred: " + error); });

Executing the loopThroughShapesInSheet function will print the names of all shapes in the active sheet to the console.

Please note that OfficeScripts are only supported in Excel for the web and Excel Online, and they may have limitations compared to other programming environments.

Pamai Tech
Turning ideas into Reality

Products

Office Add-in

Enterprise Solutions

Cloud Consulting

UI UX Design

Data Transformation

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2023 Pamai Tech