I'm trying to use a sidebar with a form to get user input. The code is bound to a Google Sheets file.
Code.gs:
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Custom Menu')
.addItem('Show sidebar', 'showSidebar')
.addToUi();
}
function showSidebar() {
var html = HtmlService.createHtmlOutputFromFile('Page')
.setTitle('My custom sidebar')
.setWidth(300);
SpreadsheetApp.getUi()
.showSidebar(html);
}
function processForm(formObject) {
var ui = SpreadsheetApp.getUi();
ui.alert("This should show the values submitted.");
}
Page.html:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
Please fill in the form below.<br><br>
<form id="myForm" onsubmit="google.script.run.processForm(this)">
First name:
<input type="text" name="firstname"><br><br>
Last name:
<input type="text" name="lastname"><br><br>
Gender:<br>
<input type="radio" name="gender" value="male" checked> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other<br>
<br>
<input type="submit" value="Submit">
</form><br>
<input type="button" value="Cancel" onclick="google.script.host.close()" />
</body>
</html>
When I press "Submit", the alert opens with the given message and my browser opens to a url which shows a blank page
(https://n-ms22tssp5ubsirhhfqrplmp6jt3yg2zmob5vdaq-0lu-script.googleusercontent.com/userCodeAppPanel?firstname=&lastname=&gender=male).
I'd like the alert to display the values submitted, and I don't want the extra page to open.
Pretty simple problem, but everything I read seems really over complicated. I just want to know the simplest way to get user input.
formObject
?Logger.log(formObject)
inprocessForm
should get you started. – Emotionality