Javascript 等待函数在GAS中执行
我有一个Google应用程序脚本代码,要求用户通过html自定义对话框输入一些值。对话框中的按钮触发了.gs文件中的一个函数,我想等待该触发函数返回,然后再继续执行其余代码Javascript 等待函数在GAS中执行,javascript,google-apps-script,google-sheets,promise,Javascript,Google Apps Script,Google Sheets,Promise,我有一个Google应用程序脚本代码,要求用户通过html自定义对话框输入一些值。对话框中的按钮触发了.gs文件中的一个函数,我想等待该触发函数返回,然后再继续执行其余代码 function main () { selectSheets (); //Below are the functions that I need openSheets to return before calling them (I'm not including their implemen
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>
不可能让服务器端代码等待客户端代码完成。IMHO您应该更改脚本的逻辑:
使用服务器端功能打开对话框/侧栏。通常这类函数不包含其他内容,因为对话框/侧栏的打开是异步的
客户端代码应该调用服务器端函数,该函数可能返回受支持的JavaScript对象。此服务器函数可用于调用应同步运行(按特定顺序)的其他服务器函数
相关的
这令人困惑。在继续代码的其余部分之前,我想等待触发的函数返回。在继续执行主程序之前。但是您只调用了openSheets
函数。对吗?为什么main
函数会运行?请删除承诺代码或显示原始代码以及承诺修改。服务器端是同步的。承诺不会起作用,也不会起任何作用。我期待着客户的承诺。无论如何,退一步,解释一下你最初的问题:你到底为什么要等待?是什么问题导致您将此作为解决方案?这是否回答了您的问题@我提供了原始代码,添加了一些有助于澄清问题的注释,并在末尾添加了一个文本来澄清我为什么需要这个。