I am new to coding and to Stack Overflow so apologies if I am not following protocol or if I am duplicating a question. I did check the questions but didn't find anything close enough with my limited knowledge.
I have copied some code from a webpage Original code is here and amended it to suit my Google sheet & html form but I kinda got stuck. I have opened view access so you can see what I am trying to do but I cannot add another link as I don't have enough reputation yet!
I have created the form and it appears on the site. If I have the code run from the Code.gs page it works in as much as it will post "undefined" in every cell where there is a header. But from the form on the site nothing happens. If I "test web app for your latest code" and F12 I get notified that it "cannot read property of 'getpubliclock'". If I comment out the public lock lines, I get informed "cannot read property of 'createTextOutput'.
My Code.gs file is:
function doGet(e) {
return HtmlService.createTemplateFromFile('myForm')
.evaluate()
.setTitle('eCPC DB v2.0')
.setSandboxMode(HtmlService.SandboxMode.NATIVE);
};
and myForm.html is:
<div>
<!-- Use a templated HTML printing scriptlet to import common stylesheet. -->
<?!= HtmlService.createHtmlOutputFromFile('Stylesheet').getContent(); ?>
<form>
<input type="text" name="FIRSTNAME" id="FIRSTNAME" class="form-control" placeholder="First Name" required>
<input type="text" name="LASTNAME" id="LASTNAME" class="form-control" placeholder="Last Name" required>
<input type="text" name="DRIVERNUMBER" id="DRIVERNUMBER" class="form-control" placeholder="Driving Licence Number" required>
<input type="email" name="EMAILADDRESS" id="EMAILADDRESS" class="form-control" placeholder="Email Address">
<input type="text" name="CONTACTNUMBER" id="CONTACTNUMBER" class="form-control" placeholder="Telephone Number">
<BR><BR>
<input type="submit" id="submit-btn" value="send" onclick=doPost() class="blue">
</form>
<script>
function doPost(e){
return handleResponse(e);
}
function handleResponse(e) {
var lock = LockService.getPublicLock();
lock.waitLock(30000);
try {
var doc = SpreadsheetApp.openById("1bEJwGt5ixDjvIUH56jcNB5j71rSetrVcvtrjMQCrVp4");
var sheet = doc.getSheetByName("DRV");
var headRow = e.parameter.header_row || 1;
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
var nextRow = sheet.getLastRow()+1;
var row = [];
for (i in headers){
if (headers[i] == "Timestamp"){
row.push(new Date());
} else {
row.push(e.parameter[headers[i]]);
}
}
sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
return ContentService
.createTextOutput(JSON.stringify({"result":"success", "row": nextRow}))
.setMimeType(ContentService.MimeType.JSON);
} catch(e){
return ContentService
.createTextOutput(JSON.stringify({"result":"error", "error": e}))
.setMimeType(ContentService.MimeType.JSON);
} finally {
// lock.releaseLock();
}
}
function setup() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
SCRIPT_PROP.setProperty("key", doc.getId());
}
</script>
I have tried running all the code from the Code.gs file and I have treid running it all within the tags on the myForm.html file but neither does anything. Can anyone advise me what I am doing wrong or point me to an example of a html form that writes to a Google spreadsheet that works so I can where I am failing?
Thank you!