Get value of html text box in Apps Script function

后端 未结 1 1260
青春惊慌失措
青春惊慌失措 2021-01-02 23:20

Something is wrong here, and all the suggestions I\'ve tried from others with similar questions don\'t seem to work.

I have two files: myPage.html and myCode.gs in

相关标签:
1条回答
  • 2021-01-03 00:01

    The error message is correct - in your Apps Script function emailTech(), there is no variable in scope that's named document.

    You've got two different ways of deploying Apps Script WebApps mixed up. Since you're using the HTML Service (and your user interface is an html file), you can't use the UI Service methods (like getElementById()) to access input values. So, you'll do something different.

    To tie the submit button and input field together, use a form, enclosed in <form> tags. The submit button will still have an onclick function, but now it will be a javascript function embedded in your HTML, which will pass all the input from the form to your emailTech() function.

    In your apps-script-side handler, you'll receive the form input as an Object, with the fields from the form as key-value pairs. The key is the name from the field.

    The general solution is described in this answer. Here's a version that fits your code. I've left out the success and failure handling that Arun shows. You should build in error checking before deploying this in real life, of course.

    Code.gs

    function doGet() {
      return HtmlService.createHtmlOutputFromFile('myPage');
    }
    
    function emailTech(form){
    
      var nameBox = form.techEmail;
      var message = "This is the text box value" + nameBox;
      MailApp.sendEmail("email@somewhere.com", "This is the subject", message );
    
    }
    

    myPage.html

    <html>
    
        <head>
            <title>Test Page</title>
        </head>
    
        <body>
            <form>
                <input type="text" value=" " name="techEmail" />
                <input type="button" onClick="formSubmit()" value="Submit" />
            </form>
        </body>
        <script type="text/javascript">
            function formSubmit() {
                google.script.run.emailTech(document.forms[0]);
            }
        </script>
    
    </html>
    
    0 讨论(0)
提交回复
热议问题