Google 脚本:如何根据邮件主题从Google 驱动器自动添加附件?

Google Script: How to automatically add the attachment from Google drive based on the email subject?

我需要脚本来自动将文件附加到电子邮件中。这些文件位于 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);
    } 
  } 
}

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

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

if (files.hasNext()) {

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

}