So I have a Google sheet, and a Google Slide, and I've used a script which extracts data from the Google sheet and adds it into a templated Google Slide.
Example of the sheet is below: Google Sheet Image
The Google slide template is: Google Slide Template
And my code is:
function generateLandingPagesReport() {
var dataSpreadsheetUrl = "https://docs.google.com/spreadsheets/d/1_hbaovbndsRXiwAzLk6zCmFJzc41v_qberzlaS185K8/edit"; //make sure this includes the '/edit at the end
var ss = SpreadsheetApp.openByUrl(dataSpreadsheetUrl);
var deck = SlidesApp.getActivePresentation();
var sheet = ss.getSheetByName('Sheet2');
var values = sheet.getRange('A2:l3').getValues();
var slides = deck.getSlides();
var templateSlide = slides[2];
var presLength = slides.length;
values.forEach(function(page){
if(page[0]){
var productImage = page[0];
var productName = page[1];
var productPrice = page[2];
var productImage2 = page[3];
var productName2 = page[4];
var productPrice2 = page[5];
var productImage3 = page[6];
var productName3 = page[7];
var productPrice3 = page[8];
var productImage4 = page[9];
var productName4 = page[10];
var productPrice4 = page[11];
templateSlide.duplicate(); //duplicate the template page
slides = deck.getSlides(); //update the slides array for indexes and length
newSlide = slides[2]; // declare the new page to update
var position = {left: 0, top: 100};
var size = {width: 150, height: 150};
var position2 = {left: 300, top: 100};
var size2 = {width: 150, height: 150};
var position3 = {left: 550, top: 100};
var size3 = {width: 150, height: 150};
var shapes = (newSlide.getShapes());
shapes.forEach(function(shape){
//a.insertImage(productImage);
shape.getText().replaceAllText('{{productName}}',productName);
shape.getText().replaceAllText('{{productPrice}}',productPrice);
//a.insertImage(productImage);
shape.getText().replaceAllText('{{productName2}}',productName2);
shape.getText().replaceAllText('{{productPrice2}}',productPrice2);
//a.insertImage(productImage);
shape.getText().replaceAllText('{{productName3}}',productName3);
shape.getText().replaceAllText('{{productPrice3}}',productPrice3);
shape.getText().replaceAllText('{{productName4}}',productName4);
shape.getText().replaceAllText('{{productPrice4}}',productPrice4);
});
newSlide.insertImage(productImage, position.left, position.top, size.width, size.height);
newSlide.insertImage(productImage2, position2.left, position2.top, size2.width, size2.height);
newSlide.insertImage(productImage3, position3.left, position3.top, size3.width, size3.height);
newSlide.insertImage(productImage4, position3.left, position3.top, size3.width, size3.height);
presLength = slides.length;
newSlide.move(presLength);
} // end our conditional statement
}); //close our loop of values
//Remove the template slide
templateSlide.remove();
}
Now my problem is, whenever in the code to input range of data, if I put in A2:L3 , i.e. 2 rows, I get correct output in the Slide, which is 2 slides with correct data.
But if I keep the range as 3 slides, A2:L4, my slides output is wrong, it would repeat the values of the first row in 2 slides.
Can anyone give me a solution to this?
CodePudding user response:
Your template slide is slides[2]. When you duplicate the new slide is slides[3].
Change
newSlide = slides[2]; // declare the new page to update
To
newSlide = slides[3]; // declare the new page to update
Otherwise you are modifying your template for the next pass. On the next pass the template slides[2] becomes new slide slides[3] and slides[3] becomes slides[4].
But don't forget templateSlide is always the same slide.
var templateSlide = slides[2];