如何使用 Apps 脚本将 Google 表格文档的每一行的图像和图表自动插入幻灯片模板(邮件合并)?

How can I automatically insert images and charts for each row of Google Sheets document to a Slides template (mail merge) using Apps Script?

我希望 Apps 脚本使用来自表格文档的数据自动生成一组新的幻灯片,其中包含我想要插入到幻灯片模板中的不同信息行,以替换占位符标签。我希望它通过一个操作立即为 table 中的每一行执行此操作,因此如果有 10 行,将生成 10 组幻灯片文档。

文本替换有效,但我不确定如何替换,例如,使用 [=16] 下的 URL 将内部写有“{{image}}”的形状替换为图像=]图片列。 图表.

也是如此
function generateNewSlides() {  

var wsID = "worksheet ID here";
var ws = SpreadsheetApp.openById(wsID).getSheetByName("Data");
var data = ws.getRange(2, 1, ws.getLastRow()-1, 6).getValues();

// the above should get the relevant table from the sheet     

data.forEach(function(info){
if(info[0]){

var firstname = info[0];
var surname = info[1];
var email = info[2];
var phone = info[3];
var image = info[4];
var presName = info[5];

// the above are columns where the different pieces of data would be taken from for the placeholders in the Slides template

var slidesTemplateID = "slides template ID here";   
var template = DriveApp.getFileById(slidesTemplateID);
var folderID = "folder where itll be saved ID here";      

var copiedTemplate = template.makeCopy(presName, DriveApp.getFolderById(folderID));
var Presentation = SlidesApp.openById(copiedTemplate.getId());

// the above should create a copy and then open it

Presentation.getSlides().forEach(function(slide) {
  slide.getShapes().forEach(function(shape) {
    shape.getText().replaceAllText("{{firstname}}",firstname);
    shape.getText().replaceAllText("{{surname}}",surname);
    shape.getText().replaceAllText("{{email}}",email);
    shape.getText().replaceAllText("{{phone}}",phone);
    shape.getText().replaceAllText("{{presname}}",presName);

  })
      // replaces the placeholder tags with the desired text    
      // I am not sure how to insert something similar for images and charts in the code here
      // I've tried variations of the below, none of which have worked
      // slide.getShapes().forEach(function(picture) {

        // picture.find("{{image}}").replace(image); 
          //  picture.findText("{{image}}").replace(image);
              //  picture.getText("{{image}}").replaceWithImage(image);        
                 // picture.getText().findText("{{image}}").replace(image);
}); 

};
});
          }

{{image}}和其他占位符的区别在于你想通过实际图像替换文本

  • 遗憾的是,您无法将图像粘贴到文本框内。

  • 相反,如果存在 {{image}} 占位符,您可以指定要将图像粘贴到包含文本框的幻灯片中。

  • 您可以使用

    检查占位符是否存在

    var imageText = shape.getText().replaceAllText("{{image}}",""); if(imageText == true){...}

  • 如果满足条件则插入图像并指定其大小和位置,例如 slide.insertImage(image).scaleHeight(0.5).scaleWidth(0.5).setLeft(10);

  • 重要提示:要在幻灯片中插入图像,您需要 webContentLink 而不是 Url,并且图像必须可公开访问(参见 here)。

  • 如果您不知道图片的 webContentLink,我建议您将电子表格中的 URL 替换为 file Id 并将代码修改为

    var id = info[4]; var image = Drive.Files.get(id).webContentLink

  • 请注意,DriveApp 无法访问 webContentLink,只能通过 Advanced Drive Service which you need to manually enable

工作样本:

function generateNewSlides() {  
  
  var wsID = "worksheet ID here";
  var ws = SpreadsheetApp.openById("xxx").getSheetByName("Data");
  var data = ws.getRange(2, 1, ws.getLastRow()-1, 6).getValues();
  
  // the above should get the relevant table from the sheet     
  
  data.forEach(function(info){
    if(info[0]){
      
      var firstname = info[0];
      var surname = info[1];
      var email = info[2];
      var phone = info[3];
      var id = info[4];
      var image = Drive.Files.get(id).webContentLink
      var presName = info[5];
      
      // the above are columns where the different pieces of data would be taken from for the placeholders in the Slides template
      
      var slidesTemplateID = "xxx";   
      var template = DriveApp.getFileById(slidesTemplateID);
      var folderID = "folder where itll be saved ID here";      
      
      var copiedTemplate = template.makeCopy(presName, DriveApp.getFolderById(folderID));
      var Presentation = SlidesApp.openById(copiedTemplate.getId());
      
      // the above should create a copy and then open it
      Presentation.getSlides().forEach(function(slide) {
        slide.getShapes().forEach(function(shape) {
          shape.getText().replaceAllText("{{firstname}}",firstname);
          shape.getText().replaceAllText("{{surname}}",surname);
          shape.getText().replaceAllText("{{email}}",email);
          shape.getText().replaceAllText("{{phone}}",phone);
          shape.getText().replaceAllText("{{presname}}",presName);
          var imageText = shape.getText().replaceAllText("{{image}}","");          
          if(imageText == true){
            slide.insertImage(image).scaleHeight(0.5).scaleWidth(0.5).setLeft(10);
          }
        })
      }); 
      
    };
  });
}