I am inserting a list of instagram handles from Google Sheets to Google Slides using Google Apps Script.
- The Google Sheets contains a list of instagram handles, such as @Apple, @Google, etc. For convenience, in the recreation below, let's assume that the handle "@Apple" has already been retrieved from the sheet as a string and placed into a variable called
handle. - The Google slide contains a single placeholder in a textbox, {{handle}}.
My goal is to insert "@Apple" into the placeholder {{handle}}, and add a hyperlink (https://www.instagram.com/apple/) to it. I.e., I want to add a hyperlink to the text I just inserted/replaced.
I am able to achieve this with the code below, but it is extremely inefficient. In my actual application, there are a lot of entries to insert, and many more shapes on the slides, so I would love to be able to do this efficiently. I am open to trying other methods too. Any guidance is appreciated.
My code:
var handle = "@Apple";
// extract username to get url
let regExp = new RegExp("[^@]*$");
var username = regExp.exec(handle)[0];
var url = "https://www.instagram.com/" + username;
// insert the handle at {{handle}} and make it a hyperlink
var allShapes = (currentSlide.getShapes());
allShapes.forEach(function(shape) {
var text = shape.getText();
var n = text.replaceAllText("{{handle}}", handle);
if (n > 0) {
var textRange = text.getTextStyle();
text.find(handle).forEach((v) => {
v.getTextStyle().setLinkUrl(url);
});
}
});


I believe your goal is as follows.
In your situation, how about using Slides API? When Slides API is used, your script can be modified as follows. I guessed that when Slides API is used, the process cost can be reduced.
In this modification, from
The Google Sheets contains a list of instagram handles, such as @Apple, @Google, etc., it supposes that several replace texts are used. The modified script is as follows.Modified script:
Please copy and paste the following script to the script editor of Google Slide. And, please enable Slides API at Advanced Google services.
First, please set
handlesfor your actual situation. In this modification, you can use multiple replacement texts. In this sample, the shapes with the text{{handle}}are replaced with@Apple. The shapes with the text{{handle2}}are replaced with@Google. And, the hyperlinks are set.handlesby one API call.Note:
References:
Added:
From your following reply,
Unfortunately, from your reply, I couldn't understand the column number from
in a single column. So, in this sample script, it supposes that it's column "A" with the header row. And also, unfortunately, I couldn't understandcreate a new slide from a template. So, in this sample script, it is supposed that the 1st-page slide in a Google Slide is a template slide. And, the 1st template slide is copied. When these are reflected in a sample script, it becomes as follows.Sample script:
In this sample script, please copy and paste the script to the script editor of Google Spreadsheet. And, please set
templateSlideIdandsheetName. It supposes that Google Slide oftemplateSlideIdhas a template slide on the 1st page. Please be careful about this.This script uses Slides API. Please be careful about this.
When this script is run, the 1st page (template slide) of the Google Slide is copied. And, the text
{{handle}}is replaced using each row of cells "A2:A" and set the hyperlinks.