GAS - 等到用户在功能继续之前输入数据

时间:2015-02-02 14:01:24

标签: javascript jquery html google-apps-script datepicker

我刚开始使用GAS,所以如果这是一个明显的问题,我会很感激任何见解并道歉。我正在尝试实现日期选择器,以便用户可以选择一些日期而不是键入它们。我这样做是使用这个问题的代码:

Returning a value using Date picker in HTMLService / Google Apps Script

这很好用,但我的问题是我的其余代码在用户输入日期之前继续运行。调用html的函数是:

function showDialog() {
  var html = HtmlService.createHtmlOutputFromFile('dateDialog')
    .setSandboxMode(HtmlService.SandboxMode.IFRAME);
  SpreadsheetApp.getUi() 
    .showModalDialog(html, 'Please provide a Date Range');
  Logger.log("HTML return = %s", html.getContent());     
  return html.getContent();
}

我从下面的主脚本文件中调用它。选择器出现但我的其余代码继续执行。在这种情况下,我有一个弹出的msgBox并关闭了datepicker。

function runthis(){
  var a = showDialog();
  Logger.log(a);

  //rest of code would go here
  Browser.msgBox('hello world');
}

我想我可以循环一些超时或暂停,直到html返回值,但这似乎是不必要的。我究竟做错了什么?谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

我想出了解决这个问题的正确方法。基本上我需要重新排序我调用不同函数的方式以及我开始编写代码的位置。也许这个问题应该被删除,或者这可能会有用,我不知道。

我从我的入口点showDialog()调用runthis(),因此runthis()中的后续代码正在执行,这是不可取的。

相反,我将我的脚本的入口点更改为showDialog(),然后从那里我将用户输入的数据传递给runthis()

非常愚蠢的错误,但希望这对使用HtmlService

的新手很有帮助

编辑: 我先调用datepicker:

function showDialog(){
  var html = HtmlService.createHtmlOutputFromFile('DateDialog')
    .setSandboxMode(HtmlService.SandboxMode.IFRAME);
  SpreadsheetApp.getUi()
    .showModalDialog(html, 'Pick a date')
}

然后在单独的html文件中调用模态对话框:

<div class="demo" >
<style type="text/css"> .demo { margin: 30px ; color : #AAA ; font-family : arial sans-serif ;font-size : 10pt } 
                        p { color : red ; font-size : 11pt } 
</style>
<link rel="stylesheet"     href="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/themes/cupertino/jquery-    ui.css">

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">    </script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>

<p>Please select a date below :</p>

<p> Start Date : <input type="text" name="StartDate" id="startdatepicker" />     </p>
<p> End Date :   <input type="text" name="EndDate" id="enddatepicker" />  </p>
<script>
$( "#startdatepicker" ).datepicker({
  showWeek: true,
  firstDay: 0,
 });
</script>
<script>
$( "#enddatepicker" ).datepicker({
  showWeek: true,
  firstDay: 0,
 });
</script>
<input type="button" value="Create" onclick="submitDates()" />
<input type="button" value="Close" onclick="google.script.host.close()" />


<script>
// Pass input dates to server-side submitDates()
function submitDates() {
  var startDate = $("#startdatepicker").val();
  var endDate = $("#enddatepicker").val();

  google.script.run
    .withSuccessHandler(
       // Dates delivered, close dialog
       function() {
         google.script.host.close();
       })
       // Display failure messages
     .withFailureHandler(
       function() {
         var div = $('<div id="error" class="error">' + msg + '</div>');
         $(element).after($("#demo"));
       })
     .submitDates(startDate,endDate);
}

</script>
</div>

将日期传递给服务器端函数,然后将数据发送到其他几个函数,在这种情况下只有一个,Timesheet(而不是runthis()

function submitDates(startDate,endDate) {
     Timesheet(startDate,endDate);
}

可能需要很多清理和简化,但还没有时间去做,只是很高兴它正在工作!