简体   繁体   English

如果单元格匹配,如何使用 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?

I created a spreadsheet that I used as checking in and checking out employees with QR Code and Google Forms. the script works and when I start it it also generates the google slides including the Qr code as an image.我创建了一个电子表格,用于使用二维码和谷歌 Forms 签入和签出员工。该脚本有效,当我启动它时,它还会生成包含二维码作为图像的谷歌幻灯片。

My problem is that when I start the script it stops automatically after 6 minutes and I can not generate all rows of the table containing information.我的问题是,当我启动脚本时,它会在 6 分钟后自动停止,而且我无法生成包含信息的表的所有行。 I want to create from row 2 to 601 (they are containing data) the slides with mailmerge.我想使用 mailmerge 创建从第 2 行到第 601 行(它们包含数据)的幻灯片。 My idea is to tell the script that it should not automatically generate all rows but only if in column (O) as example the cells for each row contain an x.我的想法是告诉脚本它不应该自动生成所有行,但仅当在列 (O) 中时,例如每行的单元格包含一个 x。 And I don't know how to do that.我不知道该怎么做。

I really dont now howHere is my script:我现在真的不知道这是我的脚本:

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[2] === "") {
      // 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[2] = 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[0] + " " + row[5] + row[4] + row[10]);

 // Replace template variables using the student's information.
    deck.replaceAllText("{{id}}", row[0]);
    deck.replaceAllText("{{tag}}", row[4]);
    deck.replaceAllText("{{besetzung}}", row[5]);
    deck.replaceAllText("{{beginn}}", row[6]);
    deck.replaceAllText("{{ende}}", row[7]);
    deck.replaceAllText("{{halle}}", row[8]);
    deck.replaceAllText("{{stand}}", row[9]);
    deck.replaceAllText("{{firma}}", row[2]);
    deck.replaceAllText("{{veranstaltung}}", row[10]);
    deck.getSlides()[0].getShapes().find(s => s.getText().asString().trim().toUpperCase() == "{{IMAGE}}").replaceWithImage(`https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=${row[13]}`);

 return deck;
}

function createCopyOfSlidesTemplate() {
 //
 var TEMPLATE_ID = "1bcIS7K-CH9KH0IixCKehFniCgAReZEH3m84pd5UxhFg";

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

From your following reply,从您的以下回复中,

I want from the spreadsheet to create the slides with the data inside.我想从电子表格中创建包含内部数据的幻灯片。 The script looks at column C if a generated link is in it from the slide.如果幻灯片中有生成的链接,脚本会查看列 C。 If not then the script generates the slide if the condition on column L is JA.如果不是,则如果 L 列上的条件是 JA,则脚本会生成幻灯片。

And, your following new request,并且,您的以下新请求,

I want to convert automatically the slides to pdf.我想自动将幻灯片转换为 pdf。

In this case, please modify mailMergeSlidesFromSheets as follows.在这种情况下,请修改mailMergeSlidesFromSheets如下。

Modified script:修改脚本:

function mailMergeSlidesFromSheets() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var dataRange = sheet.getDataRange();
  var sheetContents = dataRange.getValues();
  sheetContents.shift();
  var updatedContents = [];
  var check = 0;
  sheetContents.forEach(function (row) {
    if (row[2] === "" && row[11] === "JA") {
      check++;
      var slides = createSlidesFromRow(row);
      var slidesId = slides.getId();
      var slidesUrl = `https://docs.google.com/presentation/d/${slidesId}/edit`;
      updatedContents.push([slidesUrl]);

      slides.saveAndClose();
      var pdf = UrlFetchApp.fetch(`https://docs.google.com/feeds/download/presentations/Export?exportFormat=pdf&id=${slidesId}`, { headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() } }).getBlob().setName(slides.getName() + ".pdf");
      DriveApp.createFile(pdf); // Or DriveApp.getFolderById("###folderId###").createFile(pdf);

    } else {
      updatedContents.push([row[3]]);
    }
  });
  if (check == 0) return;
  sheet.getRange(2, 4, updatedContents.length).setValues(updatedContents);
}

暂无
暂无

声明:本站的技术帖子网页,遵循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 automatically insert images for each row of Google Sheets document to a Slides template (mail merge) using Apps Script? 如何使用邮件合并谷歌幻灯片从工作表中转换日期和时间,因为它在单元格中 - How to convert Date and Time with mailmerge google slides from sheets as it is in cell 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 表格中使用 Google Apps 脚本 - How can I have a cell that when edited publishes it's edit date next to it? In Google Sheets using a Google Apps Script 如何使用 Google Apps 脚本更新 google 表格中的单元格 - How to update a cell in google sheets 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 如何使用应用程序脚本在谷歌表格的特定单元格中设置值? - How to set a value in a particular cell in google sheets using apps script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM