繁体   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?

我创建了一个电子表格,用于使用二维码和谷歌 Forms 签入和签出员工。该脚本有效,当我启动它时,它还会生成包含二维码作为图像的谷歌幻灯片。

我的问题是,当我启动脚本时,它会在 6 分钟后自动停止,而且我无法生成包含信息的表的所有行。 我想使用 mailmerge 创建从第 2 行到第 601 行(它们包含数据)的幻灯片。 我的想法是告诉脚本它不应该自动生成所有行,但仅当在列 (O) 中时,例如每行的单元格包含一个 x。 我不知道该怎么做。

我现在真的不知道这是我的脚本:

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

从您的以下回复中,

我想从电子表格中创建包含内部数据的幻灯片。 如果幻灯片中有生成的链接,脚本会查看列 C。 如果不是,则如果 L 列上的条件是 JA,则脚本会生成幻灯片。

并且,您的以下新请求,

我想自动将幻灯片转换为 pdf。

在这种情况下,请修改mailMergeSlidesFromSheets如下。

修改脚本:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM