[英]Wait for a function to execute in GAS
我有一個谷歌應用程序腳本代碼,要求用戶通過 html 自定義對話框輸入一些值。 對話框中的按鈕會觸發.gs 文件中的 function,我想等待觸發的 function 返回,然后再繼續執行代碼的 rest。
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
}
The below function gets called through onclick in html and I want to wait for it to return before continuing the execution in the main function:
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 內部函數的 rest 之前一直檢查標志值,但是盡管 openSheets 內部的值發生了變化,但它永遠不會全局更改。
編輯:我要做的是通過 html 對話框詢問用戶 select 要處理的工作表名稱(它們應該是 select 2),然后打開這些工作表。 main ()
中調用的其余函數需要知道工作表名稱,這就是我需要等待 openSheets 返回的原因。
謝謝。
不可能使服務器端代碼等待客戶端代碼完成。 恕我直言,您應該更改腳本的邏輯:
有關的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.