使用Google Apps脚本中的DriveApp.getFileById发送多个附件

时间:2019-01-27 15:45:12

标签: email google-apps-script gmail

我正在尝试运行一个脚本,该脚本从Google表格中获取多个文件ID,并将其附加到电子邮件中。我无法使用DriveApp.getFileById附加多个文件。

该脚本运行并触发电子邮件,但没有附件。

function createAndSendDocument(values) {

  // Get the email address of the active user
  var email = Session.getActiveUser().getEmail();

  // Get the name of the document to use as an email subject line.
  var subject = 'New Paperwork';

    //Let's add the attachments variables
  var attachmentstring = values["Which documents do you want to attach?"];
  var array = attachmentstring.toString().split(",");
  var blobs = [];
  var x = 0;
  for (var i = 0; i < array.length; i++) {
   
    //check if even
    if (i % 2 === 0) {
        blobs[x] = array[i];
        x++;
    }
    
  }
  
  // Append a new string to the "url" variable to use as an email body.
  var body = 'your doc: ' + blobs[0] + blobs[1] + array[0] + array[1] + "length:" + array.length;

  // Send yourself an email with a link to the document.
  GmailApp.sendEmail(email, subject, body, {attachments: blobs});
}

我编辑了原始帖子以包含代码片段。这是Blob和数组的输出。

undefinedundefinedW-4 2019.pdf1YjQqFNzeze8VX0L6wZ9O3Y9AJNznR8jfxqlength:4

1 个答案:

答案 0 :(得分:0)

此代码有效:

function createAndSendDocument(values) {

// Get the email address of the active user
  var email = Session.getActiveUser().getEmail();

  // email subject line.
  var subject = 'New Paperwork';

    // add the attachments variables
  var attachmentstring = values["Which documents do you want to attach?"];
  var array = attachmentstring.toString().split(",");
  var blobs = [];
  var x = 0;
  for (var i = 0; i < array.length; i++) {
   
    //check if even
    if (i % 2 === 0) {

    }
    else 
    {
        blobs[x] = DriveApp.getFileById(array[i]).getBlob();
        x++;
    }
    
  }
  
  // an email body.
  var body = 'Hello';

  // Send an email with attachments
  GmailApp.sendEmail(email, subject, body, {attachments: blobs});
}