GAS - 等到用户输入数据后,功能才会继续

GAS - Wait until user enters data before function continues

本文关键字:功能 继续 数据 用户 输入 GAS      更新时间:2023-09-26

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

使用 HTMLService/Google Apps 脚本中的日期选取器返回值

这很好用,但我的问题是我的其余代码在用户可以输入日期之前继续运行。调用 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。

function runthis(){
  var a = showDialog();
  Logger.log(a);
  //rest of code would go here
  Browser.msgBox('hello world');
}

我想我可以循环一些超时或暂停,直到 html 返回值,但这似乎没有必要。我做错了什么?感谢您的任何帮助。

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

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

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

真的很愚蠢的错误,但希望这对其他不熟悉使用HtmlService的人有所帮助

编辑:我先调用日期选择器:

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);
}

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