1

I have a project to create PDF out from google form and send multiple PDF through email in Google Script,

The relevant CODE segment is as follows ,

if(TF_Samples!= null){
 //Generating Lab Reports for TAPE LIFT
 for (var Sample_Number in TF_Samples) {      
  
  //Creating Report File - Word Version
  Template_Doc = Tape_Template_Doc ;
  Report_File_Word = Template_Doc.makeCopy(Report_Folder_Word);
  Report_File_Word.setName(data[0]+"-TAPE LIFT");
  Opened_Report_Doc = DocumentApp.openById(Report_File_Word.getId());
  
  //Getting the Content of the Report & Replacing the Fields
  Report_Body = Opened_Report_Doc.getBody();
  Report_Body.replaceText("{{ Client Address }}",data[2]);
  Report_Body.replaceText("{{ Sample Date }}",data[1]);
  Report_Body.replaceText("{{ Sample Number }}",TF_Samples[Sample_Number]);
  
  //Saving & Closing the Report
  Opened_Report_Doc.saveAndClose();
  
  //Creating PDF version of Report
  Blob_PDF = Report_File_Word.getAs(MimeType.PDF);
  Report_PDF_Version = Report_Folder_PDF.createFile(Blob_PDF).setName(data[0]+"-SPORE TRAPE");
}
}

I managed to send email with single attachment but since here I am looping through a for loop I don't know how to do that.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Asanka.S
  • 61
  • 7
  • 1
    Does this answer your question? [Sending Multiple attachments with Google Script from Google Drive](https://stackoverflow.com/questions/19135885/sending-multiple-attachments-with-google-script-from-google-drive) – Marios Oct 22 '20 at 07:55
  • Number of attachments will be vary time to time and it will not easy to have this solution if there are ten , twenty attachment.But I will try to develop a array method with that.Thanks for the tip. – Asanka.S Oct 22 '20 at 08:12
  • You will put them in an array and then send that array with all the pdf attachments. You don't need to specify them one by one. – Marios Oct 22 '20 at 08:14

2 Answers2

0

Create a global array and push within each loop iteration the file into the array

Sample modification:

...
var files = [];
for (var Sample_Number in TF_Samples) {      
  ...
  Report_PDF_Version = Report_Folder_PDF.createFile(Blob_PDF).setName(data[0]+"-SPORE TRAPE");
  files.push(Report_PDF_Version);
}
GmailApp.sendEmail(recipient, subject, body, {attachments:files});
...
ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • I tried this one but not working.It is generating a error as "Exception: Invalid argument: attachments" – Asanka.S Oct 22 '20 at 09:44
  • See if modifying `files.push(Report_PDF_Version);` to `files.push(Report_PDF_Version).getAs(MimeType.PDF);` solves the issue. Just to doublecheck: `Report_File_Word` is a Google Docs file, not a Microsoft Word file, right? – ziganotschka Oct 22 '20 at 09:51
  • And does `files.push(Report_PDF_Version).getAs(MimeType.PDF);` work? – ziganotschka Oct 22 '20 at 10:08
0

as specified in the documentation, the attachment argument must be an array of blobs.

So, instead of converting the blob to a drive file just send the blobs like this :

Blob_PDF = Report_File_Word.getAs(MimeType.PDF);

and push these blobs in an array one by one.

The argument will then be valid.

Serge insas
  • 45,904
  • 7
  • 105
  • 131