简体   繁体   English

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

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

I want that Apps Script to automatically generate a new set of slides using data from a Sheets document which has rows of the different information I want inserted into a Slides template replacing the placeholder tags.我希望 Apps 脚本使用 Sheets 文档中的数据自动生成一组新的幻灯片,其中包含我想要插入到幻灯片模板中的不同信息行,以替换占位符标签。 I want it to do it instantly for each row inside the table with one action, so if there are 10 rows, 10 sets of Slides documents will be generated.我希望它通过一个操作立即为表格中的每一行执行此操作,因此如果有 10 行,将生成 10 组幻灯片文档。

The text replacement works, however I'm not sure how to replace, for example, a placeholder tag with "{{image}}"文本替换有效,但我不确定如何替换,例如,用“{{image}}”替换占位符标签

The Image is a generated Qr code in column (N) with an sheet addon (QR Code for Classroom Attendance)and for each row separate.图像是在列 (N) 中生成的 Qr 代码,带有工作表插件(课堂出勤的 QR 代码)并且每一行都是单独的。 For example 10 rows with different qr codes.例如 10 行不同的二维码。 This addon writes the generate QR code in the column N. As I said for each I have a different Qr code.这个插件在 N 列中写入生成的 QR 码。正如我所说的,每个我都有不同的 Qr 码。

function mailMergeSlidesFromSheets() {
  // Load data from the spreadsheet
  var dataRange = SpreadsheetApp.getActive().getDataRange();
  var sheetContents = dataRange.getValues();

  // Save the header in a variable called header
  var header = sheetContents.shift();

  // Create an array to save the data to be written back to the sheet.
  // We'll use this array to save links to Google Slides.
  var updatedContents = [];

  // Add the header to the array that will be written back
  // to the sheet.
  updatedContents.push(header);

  // For each row, see if the 4th column is empty.
  // If it is empty, it means that a slide deck hasn't been
  // created yet.
  sheetContents.forEach(function(row) {
    if(row[14] === "") {
      // Create a Google Slides presentation using
      // information from the row.
      var slides = createSlidesFromRow(row);
      var slidesId = slides.getId();
   
      // Create the Google Slides' URL using its Id.
      var slidesUrl = `https://docs.google.com/presentation/d/${slidesId}/edit`;

      // Add this URL to the 4th column of the row and add this row
      // to the updatedContents array to be written back to the sheet.
      row[14] = slidesUrl;
      updatedContents.push(row);
    }
  });

  // Write the updated data back to the Google Sheets spreadsheet.
  dataRange.setValues(updatedContents);

}

function createSlidesFromRow(row) {
 // Create a copy of the Slides template
 var deck = createCopyOfSlidesTemplate();

 // Rename the deck using the firstname and lastname of the student
 deck.setName(row[4] + " " + row[9] + row[3]);

 // Replace template variables using the student's information.
    deck.replaceAllText("{{id}}", row[0]);
    deck.replaceAllText("{{tag}}", row[3]);
    deck.replaceAllText("{{besetzung}}", row[4]);
    deck.replaceAllText("{{beginn}}", row[5]);
    deck.replaceAllText("{{ende}}", row[6]);
    deck.replaceAllText("{{halle}}", row[7]);
    deck.replaceAllText("{{stand}}", row[8]);
    deck.replaceAllText("{{firma}}", row[2]);
    deck.replaceAllText("{{veranstaltung}}", row[9]);
    deck.insertImage("{{image}}", row[13]);

 return deck;
}

function createCopyOfSlidesTemplate() {
 //
 var TEMPLATE_ID = "19PKvWoDtbeVHcqm4DnWUxRx1OBO817uG3cL5Ox-dQoo";

 // Create a copy of the file using DriveApp
 var copy = DriveApp.getFileById(TEMPLATE_ID).makeCopy();

 // Load the copy using the SlidesApp.
 var slides = SlidesApp.openById(copy.getId());

 return slides;
}

function onOpen() {
 // Create a custom menu to make it easy to run the Mail Merge
 // script from the sheet.
 SpreadsheetApp.getUi().createMenu("⚙️ Create BWN by Pavlos")
   .addItem("Create Slides","mailMergeSlidesFromSheets")
   .addToUi();
}
 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
    

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

I believe your goal is as follows.我相信你的目标如下。

  • You want to replace {{Image}} on 1st page of Google Slides with the image.您想要用图片替换 Google 幻灯片第一页上的{{Image}}
  • The image can be retrieved by the URL like "https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl="&M2 .可以通过 URL 检索图像,例如"https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl="&M2

In this case, how about directly using the values of column "M" as follows?在这种情况下,如何直接使用“M”列的值如下? When your script is modified, please modify it as follows.当您的脚本被修改时,请修改如下。

From:从:

deck.insertImage("{{image}}", row[13]);

To:到:

deck.getSlides()[0].getShapes().find(s => s.getText().asString().trim().toUpperCase() == "{{IMAGE}}").replaceWithImage(`https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=${row[12]}`);

Note:笔记:

  • In this modification, it supposes that row[14] === "" is true and the column "M" has the value of https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=${value} .在此修改中,它假设row[14] === ""为真,并且列 "M" 的值为https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=${value} . Please be careful about this.请注意这一点。

  • It seems that there are a lot of rows you want to process.似乎有很多行要处理。 So, if the maximum execution time (6 minutes) is over, please separate the rows for processing.所以,如果超过最大执行时间(6分钟),请分行处理。 In your this question, you want to replace {{Image}} with the image.在你的这个问题中,你想用图像替换{{Image}} So, this answer is for it.所以,这个答案是为了它。 Please be careful about this.请注意这一点。

Reference:参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 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 脚本将每一行 Google 表格文档邮件合并到幻灯片模板(邮件合并)? - How can I mailmerge for each row of Google Sheets document to a Slides template (mail merge) using Apps Script if cell match? Google 表格和 Apps 脚本上的邮件合并 - Mail Merge on Google Sheets and Apps Script 使用 Google Apps 脚本,如何替换 Google 表格模板中的文本以制作新表格? - Using Google Apps Script, how can I replace text in a Google Sheets template to make a new Sheet? 如何创建自动从 Google 表格获取数据并替换幻灯片模板中的标签的 function? - How can I create a function that automatically takes data from Google Sheets and replaces the tags in a Slides template? 使用Google Apps脚本自动在下一行插入日期 - Insert date automatically on next row available using Google apps script 如何使用 Google Apps Script 在 Google 幻灯片中设置形状的透明度/不透明度和颜色? - How can I set the transparency/opacity and color of a shape in google slides using Google Apps Script? 使用 Apps 脚本合并单元格 Google 工作表 - Merge cells Google sheets using Apps Script 如何在 Google Apps 脚本(在幻灯片中)中拆分文本(从表格中)? - How do I split text (from Sheets) in Google Apps Script (in Slides)? 如何使用Google Apps脚本将Google文档插入Google网站 - How do I insert a Google Document into a Google Site using Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM