1

Needing help with adding 'n' number of attachments.

Code below:

    for(var i = 2; i<=lr; i++)
    {
          var Contact_Person = ss.getRange(i,4).getValue();

          var PDFs = ss.getRange(i,6).getValue();

          //The above i am grabbing the PDF name from a column in My sheet.

          //This name is used for generating my Mail Merge File.
    
          var contents = DriveApp.getFolderById('1ouiQE2cERaRMC_rnSftJCqQCpr2B4x3D').getFiles();
             
          PDFs = DriveApp.getFilesByName(Contractor_Name);
          if(PDFs.hasNext())
          {

              var attach = [];

              while(contents.hasNext())
              {

                   var file = contents.next();

                   if (file.getMimeType() == "application/pdf")
                   {

                       attach.push(file);

                   }

             }
           
  
        GmailApp.sendEmail(currentEmail, Subject, messageBody,
        {

          attachments: [PDFs.next().getAs(MimeType.PDF), attach[0],attach[1],attach[2], attach[3],],

          name: 'Test'

        });
     }

//This section is for me to grab 1 attachment according to the name specified for that row matching

//the Merged file in the google drive folder.

//I am also trying to attach from a separate folder N number of files to this email.

Pdfs is a variable holding getting values from Column 6 in my Spreadsheet.

This column per row has a set of names.

When the program runs it performs a mail merge and then sends an email to the user of a specific row.

Example:

Email           Subject  Reference No.  Contact Person  Contractor Name   PDFs
***@gmail.com    Test    XXXXX          Mr. Dan         XYZ Company       XYZ Company
Nimantha
  • 6,405
  • 6
  • 28
  • 69

1 Answers1

1

Answer:

You can just push them all to the same array.

More Information:

From the documentation on GmailApp.sendEmail(recipient, subject, body, options):

Advanced parameters

attachments BlobSource[] an array of files to send with the email

So you can attach them all in one array.

Code Snippet:

if (PDFs.hasNext()) {
  var attach = [];
  attach.push(PDFs.next().getAs(MimeType.PDF));

  while (contents.hasNext()) {
    var file = contents.next();
    if (file.getMimeType() == "application/pdf") {
      attach.push(file);
    }
  }

  GmailApp.sendEmail(currentEmail, Subject, messageBody, {
    attachments: attach,
    name: "Test"
  });
}

References:

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54
  • Hi, thanks for the suggestion. The reason i dont want to grab everything is cos i am also using the same folder for other users.. Meaning that the PDF variable is used to identify the exact user and then grabs extra attachments from a different folder to send out an email to the individual users. – King Michael King Oct 02 '20 at 11:14
  • @KingMichaelKing Do you know what 'N' is in advance? – Rafa Guillermo Oct 02 '20 at 11:26
  • I'm not really sure what you mean. This will attach the first file in the folder `PDFs` and all the files inside `attach` to the email. If you only want to get N files from `attach`, how do you know what N is? – Rafa Guillermo Oct 02 '20 at 11:56
  • Hi - PDFs is actually a variable that stores the username on (column 6). var PDFs = ss.getRange(i,6).getValue(); So once the name of the user is stored inside variable PDFs, i then select the file that matches the name stored in variable PDFs. PDFs = DriveApp.getFilesByName(Contractor_Name); So this N, is determined by the PDFs variable. Perhaps i am doing it wrongly.. kindly advise if there is a better approach. 1. Folder 1 = Holds Mail Merged Files of different users with their names used to name the file. 2. Folder 2 = Holds files to be attached to all user – King Michael King Oct 02 '20 at 13:24
  • `PDFs = DriveApp.getFilesByName(Contractor_Name);` PDFs is a list of `File`s. Can you please update your question to clarify the variables so I can take another look? – Rafa Guillermo Oct 02 '20 at 13:30
  • That is not how `getFilesByName()` works. Where is `Contractor_Name`? You redefine `PDF`s without doing anything with the values you got from the Sheet so how do you expect to get the specific files pertaining to the contractor? – Rafa Guillermo Oct 02 '20 at 14:01
  • Yes correct it is a of files with different names. Each name receives a file unique to them by their name but all get a set of attachment that is common to all that sets on a different folder on Gdrive. So am trying to send a file that matches "user a" where file name is "user a" with eg. 10 files from a different folder and "user b" where file name is "user b" but the 10 attachments also is sent to "user b". – King Michael King Oct 02 '20 at 14:01
  • Where do you assign `Contractor_Name`? – Rafa Guillermo Oct 02 '20 at 14:21
  • Thank you for your update. In column 6, is there a list of PDF names, or just one PDF? – Rafa Guillermo Oct 02 '20 at 14:26
  • Hi, yes there is a list of pdf names. – King Michael King Oct 02 '20 at 22:39
  • Hi @ Rafa Guillermo, thanks again for your assistance. I actually noticed that i wasn't specifying the drive folder where to grab the users individual files. Works fine now. – King Michael King Oct 03 '20 at 02:39