0
votes

I am trying to get a script running that takes multiple file IDs from a Google Sheet and attaches them to an email. I cannot get multiple files to be attached using DriveApp.getFileById.

The script runs and fires off an email, but with no attachments.

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

I edited the original post to include the snippet of code. Here is the output of the blobs and array.

undefinedundefinedW-4 2019.pdf1YjQqFNze8VX0L6wZ9O3Y9AJNznR8jfxqlength:4

1
About your script, 1. I think that when your script is saved at the script editor, an syntax error occurs at the last line. But from The script runs and fires off an email..., can you provide the latest script? 2. About loop process of array, is this thread useful? About your situation, can you provide more information? 1. Are all file IDs in array correct values? 2. What kinds of files do you want to use? When you add the information, can you update your question? I think that those information will help users think of about your issue.Tanaike
Thank you for the response. I am fairly new to javascript and apps script, but learning. There is no syntax error. It does run and send an email. The blobs are undefined even though the array does show a length of 4 and I am thinking they are running, just not sure why they are undefined. And then, even when I didn't have the undefined issue, the blobs would not create the attachments. The user gets a form where they can pick which documents to attach. I pull the fileIds from those and then I am trying to grab them from Drive and attach them as PDF. No success yet.S. Smith
Thank you for replying and updating your question. Can I ask you about your question? 1. I cannot understand about undefinedundefinedW-4 2019.pdf1YjQqFNze8VX0L6wZ9O3Y9AJNznR8jfxqlength:4. 2. Can you provide the values of array? Of course, please remove your personal information. 3. If you want to retrieve the values of index with the even number from array and those values are the file IDs, could you please modify blobs[x] = array[i] to blobs[x] = DriveApp.getFileById(array[i]).getBlob() and try again?Tanaike
blobs[x] = DriveApp.getFileById(array[i]).getBlob() did the trick. It works! Thank you!S. Smith
Thank you for replying. I'm glad your issue was resolved. Can you post the correct script as an answer? By this, other users can see your question as the solved question.Tanaike

1 Answers

0
votes

This code worked:

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