1
votes

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.

2
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
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.soMario

2 Answers

0
votes

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});
...
0
votes

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.