Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
644 views
in Technique[技术] by (71.8m points)

javascript - 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

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 from the data recorded on the worksheet via a custom html form

I have a simple html form with email sending

this is the index html FORM file:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
      function submitForm(form) {
        google.script.run
        .withSuccessHandler(function(value){
          document.getElementById('message').innerHTML = value;
          document.getElementById('name').value = '';
          document.getElementById('email').value = '';
          document.getElementById('comment').value = '';
        }) 
        .submitData(form);
      }
    </script>
    <?!= include("css");?>
  </head>
  <body>
    <h2>Feedback Form</h2>
    <div id="message"></div>

    <br /><input id="button-responder" style="display:none;" type ="button" onclick="submitResponder();" value = "Nuova iscrizione">

    <form id="my-form">
    <br /><input id="name" type="text" name="name" placeholder="Your Name">
    <br /><input id="email" type="email" name="email" placeholder="Your Email">
    <br /><textarea id="comment" rows="10" cols="40" name="comment"></textarea>
    <br /><input id="btn" type="button" value="Submit" onclick="submitForm(this.parentNode),document.getElementById('my-form').style.display='none',submitResponder('button-responder');" />
  </form>
  <?!= include("test-js");?>  
  </body>
</html>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • 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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...