I want to create an interactive web application using html service google apps script.
I am having trouble with adding an onclick event with a function into a button.
The functions are executed when the web loads for the first time, but not when it is clicked. If I click the button later, nothing happens.
Here is the link for my project
Code.gs :
function doGet() {
var template = HtmlService.createTemplateFromFile('Index');
return template.evaluate()
.setTitle('Web App Window Title')
.setSandboxMode(HtmlService.SandboxMode.NATIVE);
}
function getmenus() {
var folder = DriveApp.getFolderById('0B5vEst6Tvg8nfmhRU1h4V044ODJlX2VxeHdKeFFicUx0UlMzMWhZcUxjTkRHSlZiR3FlT1k');
var datax = []
var result = [[]];
var filess = folder.getFilesByType(MimeType.GOOGLE_SHEETS);
while (filess.hasNext()) {
datax.push(filess.next());
}
datax.sort(function sorting(x,y){
var xp = x.getName().toLowerCase();
var yp = y.getName().toLowerCase();
return xp == yp ? 0 : xp < yp ? -1 : 1;
});
for (var a in datax) {
var ss = SpreadsheetApp.openById(datax[a].getId());
var info = ss.getSheetByName('Info');
var url = info.getRange('B1').getValue();
result[a] = [datax[a].getName(),datax[a].getId(),url];
}
return result;
}
function processmenu(result) {
var ss = SpreadsheetApp.openById(result);
var sheet = ss.getActiveSheet();
var range = sheet.getDataRange();
var values = range.getValues();
return values;
}
JavaScript.html :
<script>
google.script.run.withSuccessHandler(loadmenu).getmenus();
function loadmenu(result) {
var mainmenu = document.getElementById('menunav');
mainmenu.innerHTML='';
for (var a in result) {
var form = document.createElement('form');
var hidden = document.createElement('input');
hidden.type='hidden';
hidden.name='name';
hidden.value=result[a][1];
var button = document.createElement('input');
button.type='button';
button.name='menu';
button.value=result[a][0];
button.onclick=menuclick(result[a][1]);
form.appendChild(hidden);
form.appendChild(button);
mainmenu.appendChild(form);
var br = document.createElement("br");
mainmenu.appendChild(br);
}
}
function menuclick(result) {
google.script.run.withSuccessHandler(updateoptions).processmenu(result);
}
function updateoptions(values) {
alert(values);
}
</script>
Please help me to solve the problem and maybe how to rewrite the code in a better way.
Thanks