简体   繁体   English

Google Apps脚本-HTML服务-“等待”客户端表单提交

[英]Google Apps Scripts - HTML Service - “wait” for client-side form submission

I am creating a container-bound Google Apps Scripts that is bound to a Google Spreadsheet. 我正在创建绑定到Google Spreadsheet的容器绑定的 Google Apps脚本。 In my GS code I am using Google HTML Service to display a dialogue box in Google Sheets (ie an HTML form). 在我的GS代码中,我正在使用Google HTML Service在Google表格中显示一个对话框(即HTML表单)。

The issue I am running into is that when I render the HTML form client-side, the GS server-side code continues to run. 我遇到的问题是,当我渲染HTML表单客户端时,GS服务器端代码将继续运行。 Instead, I would like to "pause" the server side code and have it wait until the user submits the form. 相反,我想“暂停”服务器端代码,并等待用户提交表单。

Is that possible to do? 那有可能吗?

Here's what I have so far: 这是我到目前为止的内容:

Code.gs Code.gs

function onOpen(e) {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Custom')
      .addItem('Generate Shipping Email', 'menuItem1')
      .addToUi();
}

function menuItem1() {
  var html = HtmlService.createHtmlOutputFromFile('index');
  var ui = SpreadsheetApp.getUi()
  ui.showModalDialog(html, 'Shipping Email Options'); // This line shows the modal in index.html

  // Here is where I would like to "pause" until the HTML form is submitted by user clicking the button with id="button1id" ...

  Logger.log("Line after showModalDialog"); // ... however, currently this code runs before the user clicks submit or cancel button in the html form
  // Rest of code which should not run until user clicks submit or cancel button will go here
}

readyToSendEmail(shippingNeeded, notes) {
  // Some non-relevant code goes here
}

index.html 的index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <form class="form-horizontal">
    <fieldset>

    <!-- Form Name -->


    <!-- Multiple Radios (inline) -->
    <div class="form-group">
    <label class="col-md-4 control-label" for="radios">Do you need expedited shipping?</label>
    <div class="col-md-4"> 
    <label class="radio-inline" for="radios-0">
    <input type="radio" name="radios" id="shippingNeededYes" value="Yes" checked="checked">
    Yes
    </label> 
    <label class="radio-inline" for="radios-1">
    <input type="radio" name="radios" id="shippingNeededNo" value="No">
    No
    </label>
    </div>
    </div>

    <!-- Textarea -->
    <div class="form-group">
    <label class="col-md-4 control-label" for="textarea">Additional shipping notes:</label>
    <div class="col-md-4">                     
    <textarea class="form-control" id="textarea" name="textarea"></textarea>
    </div>
    </div>

    <!-- Button (Double) -->
    <div class="form-group">
    <label class="col-md-4 control-label" for="button1id">Ready to send?</label>
    <div class="col-md-8">
    <button id="button1id" name="button1id" class="btn btn-primary" onclick="clickedSubmit()">Yes, send the email</button>
    <button id="button2id" name="button2id" class="btn btn-default" onclick="google.script.host.close()">Cancel, do not send an email</button>
    </div>
    </div>

    </fieldset>
    </form>

  <script>
  function clickedSubmit() {
    if(document.getElementById('shippingNeededYes').checked) {
      var shippingNeeded = "Yes";
    } else {
      var shippingNeeded = "No";
    }
    var notes = document.getElementById('textarea');
    google.script.run.readyToSendEmail(shippingNeeded, notes);
  }
  </script>  

  </body>
</html>

Any ideas, thoughts or suggestions are appreciated! 任何想法,想法或建议,表示赞赏!

I guess you are bit confused how the js function works in app script. 我想您对js函数在应用程序脚本中的工作方式有些困惑。

//This function will run when the spreadsheet is loaded
       function onOpen(e) {
          var ui = SpreadsheetApp.getUi();
          ui.createMenu('Custom')
              .addItem('Generate Shipping Email', 'menuItem1')
              .addToUi();
        }

    //This entire function will run when you click on the menu item in the spreadsheet.
        function menuItem1() {
          var html = HtmlService.createHtmlOutputFromFile('index');
          var ui = SpreadsheetApp.getUi()
          ui.showModalDialog(html, 'Shipping Email Options'); 
        }


    //This is where you need to write the logic...this function will be called when you click on the button
       function readyToSendEmail(shippingNeeded, notes) {

        }

Also, its better to add type="button" to both the button or else it can consider it as submit buttons. 同样,最好在两个按钮上都添加type="button" ,否则可以将其视为提交按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Apps脚本表单服务 - Google Apps Scripts Form service HTML表单的客户端验证 - Client-side verification of HTML form HTML 使用 Apps 脚本登陆“谢谢”页面向 Google 表格提交表单 - HTML Form Submission to Google Sheets using Apps Scripts Landing "Thank You" Page Datetime 似乎破坏了 Google Apps Script 客户端反序列化 - Datetime seems to break Google Apps Script client-side deserialization 在Google Apps脚本中从服务器端(.gs)传递到客户端(html)时,已知类型的变量变为“未定义” - Variable of known type becomes “undefined” when passed from server side (.gs) to client-side(html) in Google Apps Script 使用JavaScript和Google App Engine中的服务器端Python代码动态生成客户端HTML表单控件 - Dynamically generate client-side HTML form control using JavaScript and server-side Python code in Google App Engine 是否有客户端从HTML表单验证静态信息的要点? - Is there a point of client-side validation of static info from a HTML form? 使用 JS 验证客户端表单 (HTML) 上的条目时出现问题 - Problem using JS to validate entry on client-side form (HTML) 流星:等待客户端排序收集 - Meteor: wait for client-side sort of collection 如何将表单数据传递到客户端函数(google.scripts.run)并将结果返回到HTML? - How to pass a form data to a client side function (google.scripts.run) and return result to HTML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM