1

What would be a good way to turn a selection of images to individual slides? This would allow one to drop a folder/multiple images into one slide, select them, and with one fell swoop, distribute them to individual slides, which saves a lot of time compared to the alternative of making a new slide and dropping an image, one-by-one.

I'm thinking something like the following, need help fixing it so that it is not pseudo code

const selection = SlidesApp.getActivePresentation().getSelection(); var currentSlide; selection.forEach( currentSlide = SlidesApp.getActivePresentation().getSelection().getCurrentPage().getObjectId(); presentation.insertSlide(currentSlide, BLANK) slide.insertImage(image) );

delete the original selection, not sure what good practice code for that would be

alternative would be to iterate through the selection and move them one by one, but that seems an unnecessary replication of how a human would do it.

I've made another script that centers existing images on slides, but stuck about here

function sortImage() {
  const slide = SlidesApp.getActivePresentation();
  //store selected slide
  let selectedSlide = SlidesApp.getActivePresentation().getSelection().getCurrentPage().asSlide();
  //gets all the images in the first slide
  let images = selectedSlide.getImages();
  // trying to get insertion index of selected slide
  let selectedSlideIndex = SlidesApp.getActivePresentation().getSelection().getCurrentPage().getObjectId();

  // iterates all images in the array
  images.forEach((image) => {
    //adds a new slide
    slide.insertSlide(selectedSlideIndex += 1)
    let current_slide = slide.getSlides()[selectedSlideIndex];

    //insert the image in the newly created slide
    current_slide.insertImage(image);

    //delete the image in the first slide. 
    image.remove()
  });
}
3
  • Are the files store in Google Drive (inside a folder in Google Drive)? Or are you pulling the images from a different location? Commented Jan 4, 2023 at 18:49
  • I can upload them to google drive, but in my original question I was drag and dropping them in a slide from the computer directly. Commented Jan 5, 2023 at 4:55
  • thanks for the update. I added an update to the answer base on that info. Commented Jan 5, 2023 at 14:35

1 Answer 1

1

I made a test to upload images to Google Slides; you mentioned that you had a script that centers existing images, so I just added a sample of how to upload an image per slide.

The sample is taking images stored inside a Google Drive folder. You can modify this depending on where your store your images.

function uploadImage() {
  const folder = DriveApp.getFolderById(folder_ID).getFiles();
  let count = 0
  const slide = SlidesApp.getActivePresentation();
  while (folder.hasNext()) {
    slide.appendSlide()
    let current_slide = slide.getSlides()[count += 1];
    let file = folder.next();
    let file_id = file.getId();
    let image = DriveApp.getFileById(file_id);
    current_slide.insertImage(image);
  }
}

enter image description here


Update:

Get all the images in the first slide. (For this example, you need to add the images in the first slide.)

function sortImage() {

  //gets all the images in the first slide
  let images = SlidesApp.getActivePresentation().getSlides()[0].getImages();
  let count = 0
  const slide = SlidesApp.getActivePresentation();

  // iterates all images in the array
  images.forEach((image) => {
    //adds a new slide
    slide.appendSlide()
    let current_slide = slide.getSlides()[count += 1];

    //insert the image in the newly created slide
    current_slide.insertImage(image);

    //delete the image in the first slide. 
    image.remove()
  });
}

enter image description here

References to list the files inside a folder:

References to add slides and insert an image:

Update:

To get the number of slides in a presentation, you can use:

let total_number_slides = slide.getSlides().length;
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you so much! Quick q. If I didn't want to use google drive and drag and dropped images in a slide, how would I go about distributing them? Something like: function imagesToSlides() { const selection = SlidesApp.getActivePresentation().getSelection(); const elements = selection.getPageElementRange().getPageElements(); let count = 0 let image = elements[count]; const slide = SlidesApp.getActivePresentation(); elements.forEach(function(value) { slide.appendSlide(); let current_slide = slide.getSlides()[count += 1]; current_slide.insertImage(image); }); }
Let me check and do some tests
@whatsnewsisyphus, I added an update with the information that you wanted.
The one shortcoming of the first slide method is that if you already have slides, it doesn't work (like adding to existing slides). Is there a way to get the index of a slide to use it as an insertion index rather than appending to the end?
I'll add it as an edit to the top in broken code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.