Reputation: 21
Google script code
HI Guys, I have having difficulty with basic code, and it seems to stop working as soon as i add google.scripts.run
your input would be greatly appreciated
The below code is my Google script code "Code.gs"
function AddRecord(name) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mainSheet = ss.getSheetByName("Main");
mainSheet.appendRow([name,new Date()]);
}
function startForm()
{
var form = HtmlService.createHtmlOutputFromFile('AddForm');
SpreadsheetApp.getUi().showModalDialog(form,'Add Record');
}
function addMenu(){
var menu = SpreadsheetApp.getUi().createMenu('Custom');
menu.addItem('Add Record Form', 'startForm');
menu.addToUi();
}
function onOpen(e){
addMenu();
}
The below code is my HTML "AddForm.HTML"
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function AddRow(){
var name = document.getElementById("name").value;
google.script.run.AddRecord(name);
}
</script>
</head>
<body>
Name: <input type="text" id="name" />
<input type="button" value="Add" onclick="google.script.run.AddRecord(name);" />
</body>
</html>
Upvotes: 0
Views: 257
Reputation: 2676
Your code seems kind of buggy.
Looking at the AddForm
file, you have created a function called AddRow
that uses the google.script.run
and that's a great way to proceed. But then you never actually used the function itself.
In the button you have:
<input type="button" value="Add" onclick="google.script.run.AddRecord(name);" />
But the variable name
is only defined in the AddRow
.
Change the onClick
to actually invoke AddRow
and you should be good.
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function AddRow(){
var name = document.getElementById("name").value;
google.script.run.AddRecord(name);
}
</script>
</head>
<body>
Name: <input type="text" id="name" />
<input type="button" value="Add" onclick="AddRow();" />
</body>
</html>
Upvotes: 2