Google Apps Script - Javascript not working

后端 未结 2 1168
灰色年华
灰色年华 2021-01-28 12:26

I created a simple web app, and it is not working.

Code.gs:

function doGet() {//I think this works
  var output = HtmlService.createTemplateFromFile(\'in         


        
相关标签:
2条回答
  • 2021-01-28 12:42

    There are quite few errors in your code.

    1. Script tag is not a self closing tag.
    2. It is safer to serve https version of jquery, sometime http content will be blocked.
    3. There is NO onclick event in jquery it is just click.

    code.gs

    function doGet() {
      var output = HtmlService.createTemplateFromFile('index').evaluate();
      output.setSandboxMode(HtmlService.SandboxMode.IFRAME);
      output.setTitle('email button');
      return output;
    }
    
    function getContent(filename) {
      return HtmlService.createHtmlOutputFromFile(filename).getContent();
    }
    

    index.html

    <!DOCTYPE html>
    <html>
      <body>
        <form id="email_subscribe">
          <input type="email" name="email" id="email" placeholder="Enter your email">
          <input type="button" id="submitButton" value="Submit">
        </form>
    
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <?!= getContent("javascriptCode") ?>
    
      </body>
    </html>
    

    javascriptCode.html

    <script type="text/javascript">
      $(document).ready(function() {
        $("#submitButton").click(function() {
          alert('thx for submitting');
        });
      });
    </script>
    
    0 讨论(0)
  • 2021-01-28 12:44

    A Simple HTML dialog example

    I usually test these things as a dialog to get them working. It's works as a dialog and I've also included the doGet. I hope this helps. I didn't go exactly as you were going but I think this will help you move that way.

    Code.gs:

    function doGet() 
    {
      var output = HtmlService.createTemplateFromFile('webappdoesnotwork');
      return output.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
    }
    
    function getEmail(email) 
    {
      Logger.log(email);
      return true;
    }
    
    function showDialog()
    {
      var output = HtmlService.createHtmlOutputFromFile('webappdoesnotwork');
      SpreadsheetApp.getUi().showModelessDialog(output, 'My WebApp');
    }
    

    webappdoesnotwork.html

    <!DOCTYPE html>
        <html>
          <head>
          <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
          <script>
          function getEmail()
          {
            var email=$('#email').val();
            google.script.run
              .withSuccessHandler(sayThanks)
              .getEmail(email);
          }
          function sayThanks()
          {
            $('#saythanks').css('display','block');
            $('#controls').css('display','none');
          }
          console.log('My Code');
        </script>
          </head>
          <body>
            <div id="saythanks" style="display:none">Hey Thanks for Responding</div>
            <div id="controls">
              <input type="text" id="email" size="35" placeholder="Enter your email"/>
              <input type="button" id="submitButton" value="Submit" onClick="getEmail();"/>
            </div>
          </body>
        </html>
    
    0 讨论(0)
提交回复
热议问题