I have a Google App Script Code code that asks the user to input some values through an html custom dialog. The button in the dialog triggers a function in the .gs file, and I want to wait for that triggered function to return before I continue the rest of the code.
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 ()
}
This is the 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>
I tried the idea of having openSheets changes a flag value and I kept checking the flag value before executing the rest of the functions inside the main, but although the value changes inside openSheets, it never changes globally.
EDIT: What I'm trying to do is to ask the user to select the sheet names to process (they should select 2) through an html dialog, then open those sheets through openSheets. The remaining functions called in main ()
needs to know the sheets names, that's why I need to wait for openSheets to return.
Thanks.