【问题标题】:Wait for a function to execute in GAS等待一个函数在 GAS 中执行
【发布时间】:2020-04-25 21:05:01
【问题描述】:

我有一个谷歌应用脚​​本代码,它要求用户通过一个 html 自定义对话框输入一些值。对话框中的按钮触发 .gs 文件中的一个函数,我想等待该触发函数返回,然后再继续其余代码。

  function main () {
      selectSheets (); 
      //Below are the functions that I need openSheets to return before calling them (I'm not including their implementation here, as I don't think it's needed):
      categoriesAndScoresDictionary = getCategoriesDictionary(); 
      categoriesEntries = getCategoriesEntries(categoriesAndScoresDictionary); 
      //and other functions..

    }

function selectSheets () {

  var htmlDialog = HtmlService.createTemplateFromFile("sheets_menu")
  var spreadsheets = SpreadsheetApp.getActiveSpreadsheet().getSheets(); 
  var sheetsArray = []; 

  for (index in spreadsheets) {
    sheetsArray.push(spreadsheets [index].getSheetName());  
  }

  htmlDialog.sheetsArray = sheetsArray; 

  var html = htmlDialog.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME).setWidth(300); 

  SpreadsheetApp.getUi().showModalDialog(html, "Select sheets names");
  return html
}

下面的函数是通过 html 中的 onclick 调用的,我想等待它返回,然后在主函数中继续执行:

function openSheets (appsSelection, rubricsSelection) {

  var spreadsheetFile = SpreadsheetApp.getActiveSpreadsheet(); 
  Logger.log(appsSelection + " " + rubricsSelection); 

  //Open the sheet by name inside the opened Spreadsheet (inside the file)
  //Open applications sheet: 
  applicationsSheet = spreadsheetFile.getSheetByName(appsSelection);

  //Open rubrics sheet: 
  rubricsSheet = spreadsheetFile.getSheetByName(rubricsSelection);
  //This function gets called from the html side and it runs successfully, I need to wait for it to return before executing other functions called in main () 

}

这是html:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <input type = "hidden" value="<?!= sheetsArray ?>" id= "sheetsArray"/>

    <br>
    <p>Select the name of the applications sheet:</p>
     <select id = "sheetsList">

     </select>
     <br>
     <p>Select the name of the rubrics sheet:</p>

     <select id = "sheetsList2">

     </select>

     <br><br><br>
    <center><input type= "button" value = "Confirm" onclick ="sendSelectionToJs()"/></center>

    <script> 
     // Create the list element:
     function fillSelection () {
     var firstMenu = document.getElementById("sheetsList"); 
     var secondMenu = document.getElementById("sheetsList2"); 

     var holderArray = document.getElementById ("sheetsArray").value; 
     var sheetsArray = holderArray.split(","); 

    for (var i = 0; i<sheetsArray.length; i++){
    var option = document.createElement("option");
    option.value = sheetsArray[i];
    option.innerHTML = sheetsArray[i];
    firstMenu.appendChild(option);    
    }

    for (var i = 0; i<sheetsArray.length; i++){
    var option = document.createElement("option");
    option.value = sheetsArray[i];
    option.innerHTML = sheetsArray[i];
    secondMenu.appendChild(option);    
    }
  }


    function sendSelectionToJs() {

    var appsSelection = document.getElementById ("sheetsList").value; 
    var rubricsSelection = document.getElementById ("sheetsList2").value; 
     google.script.run.openSheets(appsSelection, rubricsSelection); 
     google.script.host.close(); 

    }

    fillSelection (); 

    </script> 
  </body>
</html>

我尝试了让 openSheets 更改标志值的想法,并在执行 main 中的其余函数之前一直检查标志值,但是尽管值在 openSheets 内部发生了变化,但它永远不会全局更改。

编辑:我要做的是要求用户通过 html 对话框选择要处理的工作表名称(他们应该选择 2),然后通过 openSheets 打开这些工作表。 main () 中调用的其余函数需要知道工作表名称,这就是我需要等待 openSheets 返回的原因。

谢谢。

【问题讨论】:

  • 这令人困惑。 想要等待触发的函数返回,然后再继续执行其余代码。 在继续执行 main 之前。但是您只调用openSheets 函数。正确的?为什么main 函数甚至会运行?
  • 请删除承诺代码或显示原始代码以及承诺修改。服务器端是同步的。承诺不会起作用或做任何事情。我期待客户端的承诺。无论如何,退后一点,解释一下你原来的问题:你到底为什么要等待?是什么问题导致您将此作为解决方案?
  • @TheMaster 我提供了原始代码,添加了一些有助于澄清事情的 cmets,并在末尾添加了一个文本来说明我为什么需要这个。

标签: javascript google-apps-script google-sheets promise


【解决方案1】:

不可能让服务器端代码等待客户端代码完成。恕我直言,您应该更改脚本的逻辑:

  1. 使用服务器端功能打开对话框/边栏。通常此类功能不会包含其他内容,因为对话框/侧边栏的打开是异步的。
  2. 客户端代码应调用服务器端函数,该函数可能返回受支持的 JavaScript 对象。此服务器函数可用于调用应同步运行的其他服务器函数(按特定顺序)。

相关

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多