Search code examples
google-apps-scriptgoogle-slides-api

How to resize a selected image in slides?


I have a number of selected images through a bunch of presentations that I need to size to a standard size. I was wondering how do I get my script below running for just the "selected" image(s) on the current slide.

I found a solution where someone got it working for all images on the slide, but I am looking for just those objects that are selected (multiple and single).

I tried altering the code below, but to no success!

function rearrangeImages() {
  //Select current page
  var currentPage = 
SlidesApp.getActivePresentation().getSelection().getCurrentPage();
  var slide = currentPage;
  //Get selected images of CurrentPage
  var pageElements = slide.getImages();
  //For each image, change the size and position
  for (var i = 0; i < pageElements.length; i++) {
    pageElements[i].select(true);
    var image = pageElements[i];
    var imgWidth = image.getWidth();
    var newWidth = 690; 
    var imgHeight = image.getHeight();
    var forhold = imgHeight/imgWidth;
    var newHeight = forhold*newWidth;
    image.setLeft(14).setTop(14).setWidth(newWidth).setHeight(newHeight).setRotation(0);
    var ui = SlidesApp.getUi();
   // ui.alert (imgWidth);
    pageElements[i].select(false);
  }
}

Solution

    • You want to resize a selected image.

    If my understanding is correct, how about this modification?

    getSelection() directly retrieves the selected objects. In this modification, this is used.

    Modified script:

    function rearrangeImages() {
      var selectedObjects = SlidesApp.getActivePresentation().getSelection(); // Modified
      if (selectedObjects.getSelectionType() == SlidesApp.SelectionType.PAGE_ELEMENT) { // Added
        var pageElements = selectedObjects.getPageElementRange().getPageElements(); // Added
        for (var i = 0; i < pageElements.length; i++) {
          if (pageElements[i].getPageElementType() == SlidesApp.PageElementType.IMAGE) { // Added
            //For each image, change the size and position
            pageElements[i].select(true);
            var image = pageElements[i];
            var imgWidth = image.getWidth();
            var newWidth = 690; 
            var imgHeight = image.getHeight();
            var forhold = imgHeight/imgWidth;
            var newHeight = forhold*newWidth;
            image.setLeft(14).setTop(14).setWidth(newWidth).setHeight(newHeight).setRotation(0);
            var ui = SlidesApp.getUi();
           // ui.alert (imgWidth);
            pageElements[i].select(false);
          }
        }
      }
    }
    

    Note:

    • From your title, I thought that you want to resize only images. So this modified script resizes only selected images. If you want to resize the objects except for images, please remove if (pageElements[i].getPageElementType() == SlidesApp.PageElementType.IMAGE).

    References:

    If I misunderstood your question and this was not the result you want, I apologize.