Further to my original question which I posted here: UI to HTML part 1 which was answered by @SandyGood (thanks Sandy) I have now posed myself another quandary.
MyForm.HTML code is:
<!-- Use a templated HTML printing scriptlet to import common stylesheet. -->
<?!= HtmlService.createHtmlOutputFromFile('Stylesheet').getContent(); ?>
<html>
<body>
<div>
<!-- Page header title & 'completion warning -->
<span class="grey"><b>ProReactive Log Form v3.0.74</b></span>
<h4>Complete ALL fields to ensure the form is processed successfully</h4>
<form role="form" action="mail.php" method="post">
<input type="date" name="dateA" id="dateA" class="form-control" placeholder="Enter Date">
<input type="text" name="site" list="depotslist" id="site" class="form-control" placeholder="Select depot/site">
<datalist id="depotslist">
<option value="one">
<option value="two">
</datalist>
<input type="text" name="reporter" id="reporter" class="form-control" placeholder="Enter Name" style="width: 150px">
<input type="text" name="phone" id="phone" class="form-control" placeholder="Phone number" style="width: 150px">
<input type="text" name="source" list="sourcelist" id="source" class="form-control" placeholder="Select Source Code">
<datalist id="sourcelist">
<option value="one">
<option value="two">
</datalist>
<input type="text" name="hazard" list="hazardlist" id="hazard" class="form-control" placeholder="Select Hazard Code">
<datalist id="hazardlist">
<option value="one">
<option value="two">
</datalist>
<input type="text" name="brief" id="brief" class="form-control" placeholder="Enter BRIEF Details">
<input type="text" name="describe" id="describe" class="form-control" placeholder="FULL Description & Location">
<input type="text" name="priority" list="prioritylist" id="priority" class="form-control" placeholder="Select Priority" style="width: 150px">
<datalist id="prioritylist">
<option value="one">
<option value="two">
</datalist>
<button type="share" id="submit-btn" onclick="runGoogleScript()" class="btn btn-default">Submit</button>
</form>
</div>
</body>
</html>
<script>
function onSuccess(argReturnValue){
alert('was successful ' +argReturnValue);
//ResetFields on Screen
Document.getElementById("dateA").value = "";
}
function runGoogleScript() {
console.log('v3.0.74 ran!');
var d = document.getElementById("dateA").value;
var inputDa = d.toString();
var inputDe = document.getElementById("site").value;
google.script.run.withSuccessHandler(onSuccess)
.InsertInSS(inputDa);
};
</script>
and my .gs code is:
function InsertInSS(argPassedInName) {
var ssKey = '1YEx1hwzCVyhrzIMa5U4EJ0SVDkUKQ-sJBo-Id38j5vw';
var SS = SpreadsheetApp.openById(ssKey);
var Sheet = SS.getSheetByName('LOG');
Sheet.getRange(Sheet.getLastRow()+1,1,1).setValue(argPassedInName);
}
but I am missing something as I can only manage to post the first field to the spreadsheet. I have tried adding in the names of the fields in the line
.InsertInSS(inputDa);
For example
.InsertInSS(inputDa, inputDe);
and I have tried replicating the line with the different inputs e.g.
.InsertInSS(inpuDa);
.InserInSS(inputDe);
and I have tried desperate line changes that didn't make a lot of sense so I reverted them as they didn't work either. What am I not doing?
Also, I want to start posting all the data in column 2 as column 1 needs to capture the userID.
var thisUser = Session.getActiveUser().getEmail());
But where should that be inserted?
EDIT 16th July
I have inserted the suggested code, but get errors:
function runGoogleScript() {
console.log('v3.0.74 ran!');
var d = document.getElementById("dateA").value;
var inputDa = d.toString();
var inputDe = document.getElementById("site").value;
var arrayToPass = [];
arrayToPass.push(inputDa);
arrayToPass.push(inputDe);
google.script.run.withSuccessHandler(onSuccess)
.InsertInSS(arrayToPass);
};
function InsertInSS(myDataInAnArray) {
var i=0, thisValue="";
for (i=0;i<myDataInAnArray.length;i+=1) {
thisValue = myDataInAnArray[i];
var ssKey = '1YEx1hwzCVyhrzIMa5U4EJ0SVDkUKQ-sJBo-Id38j5vw';
var SS = SpreadsheetApp.openById(ssKey);
var Sheet = SS.getSheetByName('LOG');
Sheet.getRange(Sheet.getLastRow()+1,1,1,1).setValue(thisValue);
}
}
Errors produced: if I run it as it is I get
TypeError: Cannot read property "length" from undefined. (line 4, file "")
If I add in the line var myDataInAnArray = []
I get the code to run but nothing happens in the spreadsheet. A log of the array contents is blank.
Edit 21st July I'm feeling close to defeat now. Annoyingly. I have been going round in circles and I know it is my complete lack of comprehension that is hobbling me. I have changed my code in the .HTML now to:
var arrayToPass = [];
arrayToPass.push(inputDa);
arrayToPass.push(inputDe);
var myDataInAnArray = (arrayToPass)
google.script.run.withSuccessHandler(onSuccess)
Console.log(myDataInAnArray);
.InsertInSS(myDataInAnArray);
and in the .gs to:
var i=0, thisValue="";
var myDataInAnArray = [arrayToPass]
for (i=0;i<myDataInAnArray.length;i+=1) {
thisValue = myDataInAnArray[i];
var ssKey = '1YEx1hwzCVyhrzIMa5U4EJ0SVDkUKQ-sJBo-Id38j5vw';
var SS = SpreadsheetApp.openById(ssKey);
var Sheet = SS.getSheetByName('LOG');
Sheet.getRange(Sheet.getLastRow()+1,1,1).setValue(thisValue);
}
}
and all I can get is a new row entered into the spreadsheet with "undefined" in the first cell. I feel I have changed everything I can but I am so green and unskilled I admit I don't really know what I am doing. Why is the data not making it from the website form to the spreadsheet?