Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使模态对话框与异步Google脚本一起工作_Javascript_Google Apps Script - Fatal编程技术网

Javascript 使模态对话框与异步Google脚本一起工作

Javascript 使模态对话框与异步Google脚本一起工作,javascript,google-apps-script,Javascript,Google Apps Script,我需要运行一个角色数组,并为每个角色打开一个模式对话框(HTML)。我遇到了一个问题,在关闭前一个对话框之前,下一个对话框都会被打开(因为异步Google脚本) 我尝试通过为Utilities.sleep()设置一个while循环并添加一个全局变量'sleep',该变量在模式对话框关闭时变为false来实现一个解决方案 但是,现在只打开第一个对话框,代码不会在完整的“for”循环中运行 function nightStart(nightNumber, playersArray, roleList

我需要运行一个角色数组,并为每个角色打开一个模式对话框(HTML)。我遇到了一个问题,在关闭前一个对话框之前,下一个对话框都会被打开(因为异步Google脚本)

我尝试通过为Utilities.sleep()设置一个while循环并添加一个全局变量'sleep',该变量在模式对话框关闭时变为false来实现一个解决方案

但是,现在只打开第一个对话框,代码不会在完整的“for”循环中运行

function nightStart(nightNumber, playersArray, roleList) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var range = sheet.getRange("Controls!G3:G1000");
  var wakeupOrder = [];
  var sleeping;
  var role;

//collecting the array to define in what order roles wake up
  for (var i = 1; i<=20; i++) {
    var cellValue = range.getCell(i,1).getValue();
    wakeupOrder.push(cellValue);
  }

//the FOR loop that I am trying to make work (open Dialog for each role)
  for (index in wakeupOrder) {
    role = wakeupOrder[index];
    if (roleList.indexOf(role) != -1) {
      sleeping = true;
      roleWakeUp(role, playersArray, roleList);
      do {
        Utilities.sleep(2000);
        //calling global sleeping parameter that is defined as FALSE in the 'nightTargetSelection' function        
        sleeping = PropertiesService.getScriptProperties().getProperty('sleeping'); 
      } while (sleeping != false);
    }
  }   
}



//below is the function that opens the modal dialog (but the server side code still keeps running).
function roleWakeUp (role, playersArray, roleList){    
  //I have removed all code from here for Stack Overflow. The only part that I believe is important is that it opens an HTML dialog with a form
  SpreadsheetApp.getUi().showModalDialog(actionInputDlg, wakeUpText);
}



//Below function is called by the client on HTML form submission. After this form is submitted I need the next dialog to open (i.e need the Utilities.sleep to stop running
function nightTargetSelection (selected, playerNumber){
  var sleeping = false;
  PropertiesService.getScriptProperties().setProperty('sleeping', sleeping);
}
功能夜间启动(夜间号码、播放器阵列、角色列表){
var sheet=SpreadsheetApp.getActiveSpreadsheet();
var范围=sheet.getRange(“控件!G3:G1000”);
var wakeupOrder=[];
无功睡眠;
var角色;
//收集数组以定义角色唤醒的顺序
对于(var i=1;i
  • 您希望按顺序打开几个对话框
  • 在对话框上完成该过程后,您希望打开下一个对话框。
    • 也就是说,您不希望在当前作业完成之前打开下一个对话框
如果我的理解是正确的,那么这个答案呢

SpreadsheetApp.getUi()显示ModalDialog()时
按顺序打开,对话框被覆盖。我认为这是您问题的原因。因此,在这里,我想介绍一个示例脚本。在这个示例脚本中,从当前对话框打开下一个对话框。此示例脚本的流程如下。请将此视为几个答案之一

  • 通过运行
    start()
    打开一个对话框
  • 单击“确定”按钮时,通过包含下一个作业打开下一个对话框。
    • 这样,每项工作都可以完全完成
  • 所有作业完成后,
    done()
    将运行并关闭对话框
  • 示例脚本: 使用此脚本时,请将“Code.gs”和“index.html”分别复制并粘贴到脚本编辑器上的“script”和“html”中。请运行start()。此示例脚本假定您使用的是电子表格的容器绑定脚本

    Code.gs:Google应用程序脚本 index.html:html和Javascript
    
    var jobs=JSON.parse();
    var索引=数字();
    document.getElementById(“currentjob”).innerHTML=“currentjob:+jobs[index]+”,index:+index;
    函数示例(){
    如果(索引google.script.host.close()).done(“done”);
    }
    }
    
    参考资料:
    注:
    • 这是一个简单的示例脚本。请根据您的情况修改此脚本

    如果我误解了你的问题,并且这不是你想要的结果,我道歉。

    我必须道歉。我注意到我的答案中函数名的错误。所以我刚才修改了它。你能确认一下吗?
    // When all jobs were finished, this function is called.
    function done(e) {
      Logger.log(e)
    }
    
    // Open a dialog
    function openDialog(jobs, i) {
      var template = HtmlService.createTemplateFromFile('index');
      template.jobs = JSON.stringify(jobs);
      template.index = i;
      SpreadsheetApp.getUi().showModalDialog(template.evaluate(), "sample");
    }
    
    // Please run this script
    function start() {
      var jobs = ["sample1", "sample2", "sample3"];
      openDialog(jobs, 0);
    }
    
    <div id="currentjob"></div>
    <input type="button" value="ok" onclick="sample()">
    <script>
      var jobs = JSON.parse(<?= jobs ?>);
      var index = Number(<?= index ?>);
      document.getElementById("currentjob").innerHTML= "currentJob: " + jobs[index] + ", index: " + index; 
    
      function sample() {
        if (index < jobs.length - 1) {
          google.script.run.openDialog(jobs, index + 1); // Modified
        } else {
          google.script.run.withSuccessHandler(()=>google.script.host.close()).done("Done.");
        }
      }
    </script>