从Google工作表提取数据到Google幻灯片页面不工作



我有一个Google工作表和一个Google幻灯片,我使用了一个脚本从Google工作表中提取数据并将其添加到模板化的Google幻灯片中。

表格示例如下:Google工作表图片

谷歌幻灯片模板是:谷歌幻灯片模板

我的代码是:

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();

}

现在我的问题是,无论何时在代码中输入数据范围,如果我输入A2:L3,即2行,我在幻灯片中得到正确的输出,这是2张具有正确数据的幻灯片。

但是如果我将范围保持为3张幻灯片,A2:L4,我的幻灯片输出是错误的,它将重复2张幻灯片中第一行的值。

谁能给我一个解决这个问题的方法?

您的模板幻灯片是slides[2]。当你复制新幻灯片时是slides[3]。

改变
newSlide = slides[2]; // declare the new page to update

newSlide = slides[3]; // declare the new page to update

否则,您正在为下一阶段修改模板。在下一次通过时,模板幻灯片[2]变成新的幻灯片[3],幻灯片[3]变成幻灯片[4]。

但是不要忘记templateSlide总是相同的幻灯片。

var templateSlide = slides[2];

最新更新