Accessing HTML service form object

2019-07-29 06:04发布

问题:

I'm working through https://developers.google.com/apps-script/guides/html/communication trying to submit a form with info loaded from a google sheet. On the client side I have (Based heavily on the form example in the article) :

<!DOCTYPE html>
<html>
<head>
    <base target="_top">
    <script>
        // Prevent forms from submitting.
        function preventFormSubmit() {
          var forms = document.querySelectorAll('form');
          for (var i = 0; i < forms.length; i++) {
            forms[i].addEventListener('submit', function(event) {
              event.preventDefault();
            });
          }
        }
        //      window.addEventListener('load', preventFormSubmit);

        function handleFormSubmit(formObject) {
          google.script.run.withSuccessHandler(updateUrl).processForm(formObject);
        }
        //      function updateUrl(url) {
        //        var div = document.getElementById('output');
        //        div.innerHTML = '<a href="' + url + '">Got it!</a>';
        //      }
    </script>
</head>
<body>
    <form id="myForm" onsubmit="handleFormSubmit(this)">
        <div>
            <select id="optionList" name="email">
                <option>Loading...</option>
            </select>
        </div>
        <br>
        <div>
            <textarea name="message" rows="10" cols="30">
            The cat was playing in the garden.
            </textarea> 
        </div>
        <input type="submit" value="Submit" />
    </form>

On the server side (code.gs) I have:

function processForm(formObject) {

  Logger.log('in here');
  var formBlob = formObject.myFile;
  var driveFile = DriveApp.createFile(formBlob);

return driveFile.getUrl();
}

I can see that the submit is working because I see 'in here' in the logs. How can I access the form fields from within the processForm function?

回答1:

This chunk doesn't make sense to me

  var formBlob = formObject.myFile;

Your form doesn't contain the input whose 'name' attribute is set to 'myFile'. Once you click 'submit', the 'formObject' variable will be:

{
 email: "Loading...", //from <select id="optionList" name="email">
 message: "The cat was playing in the garden." //from  <textarea name="message">

}