谷歌脚本:如何自动添加附件从谷歌驱动器基于电子邮件的主题



我需要脚本自动将文件附加到电子邮件。这些文件位于谷歌驱动器中。文件名称与邮件主题名称一致。每个科目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);
    } 
  } 
}

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

var files = DriveApp.getFilesByName(rowData.subject);
if (files.hasNext()) {
  MailApp.sendEmail(rowData.emailAddress1, rowData.subject, emailText, {
      attachments: [files.next().getBlob()]
  });
}

相关内容

  • 没有找到相关文章

最新更新