0

How do I get the same result using google Script, not with the Google Script API. I am trying to replace Text with Images on Google Slides using Google Script. Thank you in advance...

function insertImageCentered()
{
  var presentation = SlidesApp.getActivePresentation();
  
  var requests = [{
    "replaceAllShapesWithImage": {
      "imageUrl": "https://docs.google.com/drawings/d/e/2PACX-1vR5mi6ujksb_2WtTFmk39IPYBIBlJ6WkzM1nsys9cT4Wquik627DDIRXzoYTgHPKX3fcvJzG9inDmJt/pub?w=960&h=720",
        "imageReplaceMethod": "CENTER_INSIDE",
        "pageObjectIds": [presentation.getSlides()[0].getObjectId()],
        "containsText": {
          "text": "{{CENTERED_SHAPE}}",
          "matchCase": false
        }
     }
   }];
  
   Slides.Presentations.batchUpdate({'requests': requests}, presentation.getId());
}
0

1 Answer 1

2

I believe your goal as follows.

  • You want to convert your script using Google Slides API to the script using Slides Service without using Slides API.
    • From How do I get the same result using google Script, not with the Google Script API., I understood that Google Script API you are thinking might be meaning Google Slides API. By this, I thought your goal like above.

In this case, I thought that the method of replaceWithImage can be used. When this method is used, the script becomes as follows.

Sample script:

Please copy and paste the following script to the script editor of the Google Slides. And, please run the function of myFunction.

function myFunction() {
  var searchText = "{{CENTERED_SHAPE}}";
  var imageUrl = "https://docs.google.com/drawings/d/e/2PACX-1vR5mi6ujksb_2WtTFmk39IPYBIBlJ6WkzM1nsys9cT4Wquik627DDIRXzoYTgHPKX3fcvJzG9inDmJt/pub?w=960&h=720";

  // 1. Retrieve 1st slide.
  var presentation = SlidesApp.getActivePresentation();
  var slide = presentation.getSlides()[0];

  // 2. Replace the shape which has the text of "searchText" with the image of "imageUrl".
  slide.getShapes().forEach(s => {
    if (s.getText().asString().toLocaleUpperCase().includes(searchText.toLocaleUpperCase())) {
      s.replaceWithImage(imageUrl);
    }
  });
}
  • In your script, "matchCase": false is used. At above script, I used if (s.getText().asString().toLocaleUpperCase().includes(searchText.toLocaleUpperCase())) { for this. When you want to use "matchCase": true, please modify it to if (s.getText().asString().includes(searchText)) {.
  • And, if you want to replace the shape which has only the text of {{CENTERED_SHAPE}} with the image, please modify it to if (s.getText().asString().trim() == searchText) {.

Reference:

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

1 Comment

Thanks for the quick response @Tanaike. What do you mean according to what I need. The code above works. But I found a new problem. 1. The slides I use are more than one 2. Image appears on the top left. Is there a way to set the position of the Image?

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.