Google app script on how to create a pdf from the data received from an html form of its spreadsheet and send the pdf file via email

后端 未结 1 1631
遇见更好的自我
遇见更好的自我 2020-12-20 08:59

Google app script on how to create a pdf from the data received from an html form of your spreadsheet and send the pdf file via email - I would like to create a pdf file fro

相关标签:
1条回答
  • 2020-12-20 09:13
    • You want to convert var body=Utilities.formatString('name: %s <br />Email: %s<br />Comment: %s', form.name,form.email,form.comment); as a PDF file.
    • You want to put the created PDF file to the specific folder in Google Drive.
    • You want to send the created PDF file as an attachment file.
    • You want to retrieve the URL of created PDF file and return it to HTML side.
    • Your question is unrelated to Spreadsheet.

    I could understand like above. If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

    Modification points:

    • In your case, I think that your goal can be achieved by modifying submitData() at Google Apps Script side.
    • When the button is pushed, create a PDF data from var body=Utilities.formatString('name: %s <br />Email: %s<br />Comment: %s', form.name,form.email,form.comment);. And create the PDF data as the file.

    Modified script:

    Please modify submitData() as follows. Please set the folder ID to var folderId = "###".

    function submitData(form) {
      var subject='New Feedback';
      var body=Utilities.formatString('name: %s <br />Email: %s<br />Comment: %s', form.name,form.email,form.comment);
    
      var folderId = "###"; // Please set the folder ID.  // Added
      var blob = Utilities.newBlob(body, MimeType.HTML, form.name).getAs(MimeType.PDF);  // Added
      var file = DriveApp.getFolderById(folderId).createFile(blob);  // Added
    
      var aliases = GmailApp.getAliases()
       Logger.log(aliases); //returns the list of aliases you own
       Logger.log(aliases[0]); //returns the alias located at position 0 of the aliases array
    
      GmailApp.sendEmail('my-email@my-email.com','From an alias', 'A message from an alias!', {'from': aliases[0],subject: subject,htmlBody: body, attachments: [blob]});  // Modified
    
      return file.getUrl();  // Modified
    }
    

    Note:

    • If you want to modify the style of PDF file, please modify name: %s <br />Email: %s<br />Comment: %s of var body=Utilities.formatString('name: %s <br />Email: %s<br />Comment: %s', form.name,form.email,form.comment);. You can use HTML in this case.
    • By the situation, it might be required to share the created PDF file.

    References:

    • Class Utilities
    • Class DriveApp
    • getAs(contentType)
    0 讨论(0)
提交回复
热议问题