1

I would like to replace some text box text in Google slides with corresponding images. I have tried to following method: How to replace Text with Image on Google Slide using Google Script

It only works on small amounts of data, but I have over 500 object to loop through. I cannot use the Slides API only apps script.

Are there any efficient ways to go about it?

Here is my code so far:

function addFruits() {
  //adding search texts
  var apple = "Apple";
  var orange = "Orange";
  var cherry = "Cherry";
  var bamboo = "Bamboo";

  //addding file ids
  var appleID = 'XXXXXXX';
  var orangeID = 'XXXXXXXXXXX';
  var cherryID = 'XXXXXXXXX';
  var bambooID = 'XXXXXXXXXX';

  //getting images
  var appleIMG = DriveApp.getFileById(appleID).getBlob();
  var orangeIMG = DriveApp.getFileById(orangeID).getBlob();
  var cherryIMG = DriveApp.getFileById(cherryID).getBlob();
  var bambooIMG = DriveApp.getFileById(bambooID).getBlob();
  //Logger.log(appleIMG)
  //Logger.log(orangeIMG)
  //Logger.log(cherryIMG)
  //Logger.log(bambooIMG)

  // retreive all slides.
  var presentation = SlidesApp.openById(slideID);
  var slides = presentation.getSlides();
  //Logger.log(slides)

  //get all shapes and reduce them to a 1d array
  var shapesArrays = [];
  slides.forEach(slide => {
    let shape = slide.getShapes();
    shapesArrays.push(shape)
  })
  var shapes = [].concat.apply([], shapesArrays);
  //Logger.log(shapes)

 
  shapes.forEach(s => {
    if (s.getText().asString().includes(apple)) {
      s.replaceWithImage(appleIMG);
    }
    else if (s.getText().asString().includes(orange)) {
      s.replaceWithImage(orangeIMG);
    }
    else if (s.getText().asString().includes(cherry)) {
      s.replaceWithImage(cherryIMG);
    }
    else if (s.getText().asString().includes(bamboo)) {
      s.replaceWithImage(bambooIMG);
    }
  });
}
6
  • Can you elaborate, why you cannot use the Slides API only apps script? I would expect the opposite Commented Aug 15, 2021 at 12:06
  • for the Google Slides API the call needs an images with is stored on the internet and I cannot upload the image to the net. Commented Aug 15, 2021 at 12:12
  • What is the specific problem, do you get any errors? I assume you hit the storage limit or something by storing that many images as blobs Commented Aug 15, 2021 at 12:16
  • it's an information security issue. I literally cannot access an outside image hosting service, and I'm fully dependent of a simple apps script solution vs a slides API solution. Commented Aug 15, 2021 at 12:22
  • Hm, but you can get the Url of each of the files stored on your Google drive. Maybe you can access them? Commented Aug 15, 2021 at 12:37

1 Answer 1

2

Try this:

function addFruits() {
  const ids = [{ id: "id1", name: "name1",{ id: "id2", name: "name2" }];
  var presentation = SlidesApp.openById(slideID);
  var slides = presentation.getSlides();
  slides.forEach(slide => {
    let shapes = slide.getShapes();
    shapes.forEach(shape => {
      let text = shape.getText().asString();
      ids.forEach(e => {
        if (text.includes(id.name)) {
          shape.replaceWithImage(DriveApp.getFileById(e.id).getBlob());
        }
      });
    });
  });
}

You can limit the number of slides you attempt by limiting the size of ids

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, I did try this, but it still has the same issue my original code does: it times out before it finishes execution, because of the API calls.
Yeah well 200 slides is a lot of slides you probably need to them in batches. It takes time to open files load images. and there's nothing you can do about that. How many did it complete?
Oh well that's pretty good so it looks like you can do it is about 5 batches.
What really interest me is if it was possible to replace the text outside of a loop, so as to reduce the o notation, and just set the images on the slide

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.