I want to record user input under specific categories - for this I wish to have the user select a category from a drop down list, and also have another text cell where they can enter text.
My goal is to populate the drop down with specific criteria, then record the users selection and additional text string into variables that are then written into the spreadsheet.
EDIT: I have been able to create the drop down and populate it - however the selected input from the drop down is only returning to the spreadsheet very sporadically (it has worked twice in about 30 attempts).
EDIT2: If I remove google.script.host.close();
from the code then the var is passed back to the spreadsheet. It appears that the dialog box is closing too quickly, but sleep()
in the functionToRunOnFormSubmit(fromInputForm)
function is not delaying the code at all.
Code.js:
function fncOpenMyDialog() {
//Open a dialog
var htmlDlg = HtmlService.createHtmlOutputFromFile('addDeck')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setWidth(200)
.setHeight(150);
SpreadsheetApp.getUi()
.showModalDialog(htmlDlg, 'Add a New Deck');
};
function functionToRunOnFormSubmit(fromInputForm) {
Logger.log(fromInputForm);
var ss = SpreadsheetApp.getActive();
ss.getSheetByName("test").getRange(2, 1, 1, 1).setValue(fromInputForm);
};
addDeck.html
<!DOCTYPE html>
<html>
<body>
<form>
<select name="Class" id="class-selector" autofocus="autofocus" autocorrect="off" autocomplete="off">
<option value="" selected="selected">Class</option>
<option value="Druid">Druid</option>
<option value="Hunter">Hunter</option>
<option value="Mage">Mage</option>
<option value="Paladin">Paladin</option>
<option value="Priest">Priest</option>
<option value="Rogue">Rogue</option>
<option value="Shaman">Shaman</option>
<option value="Warlock">Warlock</option>
<option value="Warrior">Warrior</option>
</select>
<input type="submit" value="Submit" onclick="myFunction()">
</form>
<p id="addDeck"></p>
<script>
function myFunction() {
var x = document.getElementById("class-selector").value;
document.getElementById("addDeck").innerHTML = x;
google.script.run
.functionToRunOnFormSubmit(x);
google.script.host.close();
}
</script>
</body>
</html>
Code sourced primarily from Using an HTML drop-down menu with Google Apps Script on Google Sheets - also from How to create a drop down list in (App Script) Spreadsheet Input Box?
I still need to add a text cell for user input and return that value to the spreadsheet.
Any help would be greatly appreciated, thank you!