Using email address from external HTML file to sen

2019-09-20 12:06发布

问题:

I'm creating a function within Google Apps whereby I need to email the spreadsheet I am working on as a PDF. The user should click on a button and a dialogue box should open whereby they can enter email address, subject and message and send the email from there.

I've created the email dialogue as a sidebar with the following HTML:

<!DOCTYPE html>
<html>
    <head>
       <link rel = "stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
       <base target="_top">
    </head>
    <body>
       <form>
         <div class="form-group">
           <label for="exampleInputEmail1">Email address</label>
           <input type="email" class="form-control" id="exampleInputEmail1" ar.  ia-describedby="emailHelp" value="d.bage@group-miki.com">
         </div>
         <div class="form-group">
            <label for="subject">Subject</label>
            <input type="input" class="form-control" id="subject" value="Contract    renewal - Miki Travel">
         </div>
         <div class="form-group">
             <label for="subject">Message</label>
             <textarea class="form-control" rows="12" id="text"></textarea>
         </div>

         <button type="submit" class="btn btn-primary">Submit</button>
       </form>
    </body>
</html>

However, when I'm writing the script to send the email, how do I store the content of the email box here as variable to use for sending the email? The same will apply obviously to the subject and the message itself.

回答1:

1) Define name properties for your inputs. Currently, none of your form fields has the 'name' property. Here's the example of how it should look like

   <input type="email" name="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" value="d.bage@group-miki.com">

This will allow you to access the properties of the form object when the form is submitted (e.g., form.email, form.subject, etc);

2) On the client-side (html), you must write the code to handle the 'submit' event. Add the following before the closing </body> tag:

<script>


window.onload = function() {


    document.getElementsByTagName('form')[0]
            .addEventListener('submit', function(e){

              e.preventDefault(); // intercept redirect to another page
              google.script.run.handleFormSubmit(this); // pass the form object to the server-side function. 



            });

 }



 </script>

You can also use jQuery to achieve the same result. Here's my server-side (.gs) function that logs the contents of the input fields

function handleFormSubmit(form) {

  Logger.log(form.email);
  Logger.log(form.subject);
  Logger.log(form.message);

}

Use google.script.run for client-server communication. See the docs for more details https://developers.google.com/apps-script/guides/html/communication



回答2:

You can send pdf but Google App script does support below extension file .xls and .xlsx in mail service of google API through google app script its only supports pdf and image format attach file go through this link u will get more info regrading this

https://developers.google.com/apps-script/reference/base/blob#getAs(String)

if u still try to attach .xlsx and .xls file it through an error its does not support i already faced this issue but u can download as xlsx format through javascript.