Home > OS >  How to replace a placeholder in Google Slide with an Image from a Image URL in Sheet
How to replace a placeholder in Google Slide with an Image from a Image URL in Sheet

Time:04-01

I would like to replace a placeholder in Google Slide with an image, taken from an Image Url from Google Sheet.

I have a script that replaces text placeholders and creates a Google slide with the text information for every row in Google Sheet.

I would like to add the right code so that the script takes also the URL image link from Google Sheet and replaces the image placeholder in Google Slide with the imbedded URL image from Sheet, with every row representing a slide.

This is my code below

values.forEach(function(page){
if(page[0]){

var landingPage = page[0];
var sessions = page[1];
var newSessions = page[2];
var pagesPer = page[5];
var goalRate = page[7];
var goalValue = page[9];

defaultSlides = slides.getSlides();
defaultSlide = defaultSlides[1];
newSlide = defaultSlide;
newSlide2 = defaultSlides[2];

 var shapes = (newSlide.getShapes());
 shapes.forEach(function(shape){
   shape.getText().replaceAllText('{{landing page}}',landingPage);
   shape.getText().replaceAllText('{{sessions}}',sessions);
   shape.getText().replaceAllText('{{new sessions}}',newSessions);
   shape.getText().replaceAllText('{{pages per session}}',pagesPer);
   shape.getText().replaceAllText('{{goal rate}}',goalRate);
   shape.getText().replaceAllText('{{goal value}}',goalValue);
});
}

In the code, "page" represents a column in Google Sheet.

I would like create a column in Sheet for the Image URL, and write in the script as such, for example:

var imageUrl = page[10],

and instead of replaceAllText, I would like to replace the Google Slide placeholder with the Image URL in Google Sheet.

Thank you for your help

CodePudding user response:

I believe your goal is as follows.

  • For 2nd page of the Google Slides, you want to replace texts of "{{landing page}}", "{{sessions}}", "{{new sessions}}", "{{pages per session}}", "{{goal rate}}", "{{goal value}}" with the texts.
  • You want to replace a text with an image retrieved from var imageUrl = page[10].

From your question, I couldn't confirm the text for replacing the image. So in this answer, as a sample placeholder, {{image}} is replaced with the image of URL from var imageUrl = page[10].

In this case, how about the following modification?

Modified script:

In this script, {{image}} is replaced with the image of URL. So please put {{image}} to the 2nd page of the Slides.

var values = ###; // Please set your values.
var slides = SlidesApp.getActivePresentation(); // or SlidesApp.openById("###");

var defaultSlides = slides.getSlides();
values.forEach(function (page) {
  if (page[0]) {
    var landingPage = page[0];
    var sessions = page[1];
    var newSessions = page[2];
    var pagesPer = page[5];
    var goalRate = page[7];
    var goalValue = page[9];
    var imageUrl = page[10];
    var newSlide = defaultSlides[1];
    var texts = [
      ["{{landing page}}", landingPage],
      ["{{sessions}}", sessions],
      ["{{new sessions}}", newSessions],
      ["{{pages per session}}", pagesPer],
      ["{{goal rate}}", goalRate],
      ["{{goal value}}", goalValue]
    ];
    texts.forEach(t => newSlide.replaceAllText(...t));
    newSlide.getShapes().forEach(s => {
      if (s.getText().asString().trim() == "{{image}}") s.replaceWithImage(imageUrl);
    });
  }
});
  • When this script is run, in the 2nd page of the Slides, the texts of "{{landing page}}", "{{sessions}}", "{{new sessions}}", "{{pages per session}}", "{{goal rate}}", "{{goal value}}" are replaced with landingPage, sessions, newSessions, pagesPer, goalRate, goalValue. And, {{image}} is replaced with the image of the URL from var imageUrl = page[10].

Note:

  • In the loop of your script, the 1st element of values is used to 2nd page of Google Slides. This is from your script. Please be careful this.

  • In order to replace a text with a text, I used texts array and forEach.

  • In order to replace a text with an image, I used replaceWithImage by searching the texts of {{image}}.

  • If 2nd page of Slides has no placeholders and {{image}} is not set and those placeholders are not set, your goal cannot be achieved. Please be careful this.

Reference:

  • Related