Trying to write a script that will replace a piece of text on Google slides by an image sourced from a URL in a google sheets list. My script works but only for 1 text. How do I replace multiple texts on the slide ? Not just one. The error message I'm getting is : Page element is not of type shape.
Here is my script
function myFunction() {
var brandlogosheet = "https://docs.google.com/spreadsheets/d/1kTEoHil6FFLh2ydXaJ9RlU2n5uv9KPjrAhbQY_8hZXE/edit#gid=1309887859"
var ss = SpreadsheetApp.openByUrl(brandlogosheet);
var sheet = ss.getSheetByName('brandlogoscript');
var values = sheet.getRange('A2:DY3').getValues();
var slides = SlidesApp.openById("1pFq0ogk1g1_rXxkahOuvAnksysWShozOet6CzF4BcIk"); // SlidesApp.getActivePresentation();
var defaultSlides = slides.getSlides();
values.forEach(function (page) {
if (page[0]) {
var a = page[0];
var b = page[1];
var c = page[2];
var d = page[3];
var e = page[4];
var f = page[5];
var g = page[6];
var h = page[7];
var i = page[8];
var j = page[9];
var k = page[10];
var l = page[11];
var newSlide = defaultSlides[8];
newSlide.getShapes().forEach(s => {
if (s.getText().asString().trim() == "{{T1_Marque1}}") s.replaceWithImage(a);
//if (s.getText().asString().trim() == "{{T1_Marque2}}") s.replaceWithImage(b); // here is where it doesn't work
});
}
});
}
I GET THE ERROR : Page element is not of type shape.
I would appreciate some help. I feel like I'm missing something. Thank you !
From your following script,
newSlide.getShapes().forEach(s => {
if (s.getText().asString().trim() == "{{T1_Marque1}}") s.replaceWithImage(a);
//if (s.getText().asString().trim() == "{{T1_Marque2}}") s.replaceWithImage(b); // here is where it doesn't work
});
I guessed that in your Google Slide, the shapes including the texts of {{T1_Marque1}}
and {{T1_Marque2}}
are replaced with the image. In this case, the following flow is run in the above script.
if (s.getText().asString().trim() == "{{T1_Marque1}}") s.replaceWithImage(a);
converts a shape includeing the text of {{T1_Marque1}}
to an image.
The same shape is trying to use as the shape with if (s.getText().asString().trim() == "{{T1_Marque2}}") s.replaceWithImage(b);
. In this case, the shape has already been converted to an image. By this, an error like Page element is not of type shape.
occurs.
I guessed that the reason for your current issue is this.
In order to avoid this issue, how about the following modification?
newSlide.getShapes().forEach(s => {
if (s.getText().asString().trim() == "{{T1_Marque1}}") s.replaceWithImage(a);
//if (s.getText().asString().trim() == "{{T1_Marque2}}") s.replaceWithImage(b); // here is where it doesn't work
});
newSlide.getShapes().forEach(s => {
var obj = { "{{T1_Marque1}}": a, "{{T1_Marque2}}": b };
var text = s.getText().asString().trim();
if (obj[text]) s.replaceWithImage(obj[text]);
});
a
and b
are used by the text in the shape.