Google Script:如何根据电子邮件主题自动添加来自Google云端硬盘的附件?

时间:2016-09-28 08:40:42

标签: email google-sheets attachment

我需要脚本自动将文件附加到电子邮件中。这些文件位于Google云端硬盘中。文件名与电子邮件主题相同。每个科目1个PDF。请帮忙!

部分代码是:

  function sendEmails() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var dataSheet = ss.getSheets()[0];
  var dataRange = dataSheet.getRange(2, 1, dataSheet.getMaxRows() - 1, 1000);

  var templateSheet = ss.getSheets()[1];
  var emailTemplate = templateSheet.getRange("A1").getValue();

  objects = getRowsData(dataSheet, dataRange);

  for (var i = 0; i < objects.length; ++i) {
    var rowData = objects[i];


    var emailText = fillInTemplateFromObject(emailTemplate, rowData);
    if (rowData.emailAddress1 != null) {
    MailApp.sendEmail(rowData.emailAddress1, rowData.subject, emailText);
    }
     if (rowData.emailAddress2 != null) {
    MailApp.sendEmail(rowData.emailAddress2, rowData.subject, emailText);
    } 
    if (rowData.emailAddress3 != null) {
    MailApp.sendEmail(rowData.emailAddress3, rowData.subject, emailText);
    } 
    if (rowData.emailAddress4 != null) {
    MailApp.sendEmail(rowData.emailAddress4, rowData.subject, emailText);
    } 
    if (rowData.emailAddress5 != null) {
    MailApp.sendEmail(rowData.emailAddress5, rowData.subject, emailText);
    } 
    if (rowData.emailAddress6 != null) {
    MailApp.sendEmail(rowData.emailAddress6, rowData.subject, emailText);
    } 
    if (rowData.emailAddress7 != null) {
    MailApp.sendEmail(rowData.emailAddress7, rowData.subject, emailText);
    } 
    if (rowData.emailAddress8 != null) {
    MailApp.sendEmail(rowData.emailAddress8, rowData.subject, emailText);
    } 
    if (rowData.emailAddress9 != null) {
    MailApp.sendEmail(rowData.emailAddress9, rowData.subject, emailText);
    } 
    if (rowData.emailAddress10 != null) {
    MailApp.sendEmail(rowData.emailAddress10, rowData.subject, emailText);
    } 
  } 
}

1 个答案:

答案 0 :(得分:0)

您可以根据主题在云端硬盘中找到该文件,并将其添加为附件。

var files = DriveApp.getFilesByName(rowData.subject);

if (files.hasNext()) {

  MailApp.sendEmail(rowData.emailAddress1, rowData.subject, emailText, {
      attachments: [files.next().getBlob()]
  });

}