Grabbing multiple attachments with a specific file name in Google Appscript

荒凉一梦 提交于 2021-01-28 05:50:47

问题


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

Any help is greatly appreciated.

Thank you.


回答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"
  });
}

I hope this is helpful to you!

References:

  • Class GmailApp - sendEmail(recipient, subject, body, options) | Apps Script | Google Developers


来源:https://stackoverflow.com/questions/64165255/grabbing-multiple-attachments-with-a-specific-file-name-in-google-appscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!