I'm trying to use multiple selector to post data to google sheet using google apps script as follows:
Code.gs:
function doGet() {
return HtmlService.createTemplateFromFile('Form.html').evaluate()
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
function doPost (e) {
var lock = LockService.getScriptLock();
lock.tryLock(10 * 1000);
try {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var sheet = doc.getSheetByName("Sheet1");
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]
var nextRow = sheet.getLastRow() + 1
var newRow = headers.map(function(header) {
return header === 'Timestamp' ? new Date() : e.parameter[header]
})
sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow])
return HtmlService.createTemplateFromFile('Confirmation.html').evaluate()
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
catch (e) {
return ContentService
.createTextOutput(JSON.stringify({ 'result': 'error', 'error': e }))
.setMimeType(ContentService.MimeType.JSON)
}
finally { lock.releaseLock() }
}
Form.html:
<!DOCTYPE html>
<html lang="en">
<body>
<form name="myForm" action="https://script.google.com/macros/s/AKfycbwamMCkOCAmwrRDNj8cTqgp3cevm8B9niyrlYa5dnbxP2H3q0o/exec" target="_self" method="POST">
<div>
<select name="M Values" multiple required>
<option value="" disabled selected>Select Value(s)</option>
<option value="M1">M1</option>
<option value="M2">M2</option>
<option value="M3">M3</option>
<option value="M4">M4</option>
<option value="M5">M5</option>
<option value="M6">M6</option>
</select>
</div>
<button type="submit">Send</button>
</form>
</body>
</html>
The problem is when a user chooses many options for example M1, M3, and M5 and click the submit button, I only receive M1 in the sheet instead of receiving all the selected values like that M1, M3, M5
Sheet: https://docs.google.com/spreadsheets/d/1FdOH2zU_ZnxB2a7RA_QfO3HdPTRhVhSHo5988a8Au9s/edit?usp=sharing
The sheet is editable so please feel free to use. Please any help is very appreciated. Thanks in advance
I tried to use e.postData.contents but it gives me the error: TypeError: Cannot read property 'postData' of undefined
, in this case,e
isundefined
. So I'm worry that you might directly run the function ofdoPost
. How about this? If my guess is not correct, can you provide the detail flow for replicating your issue ofTypeError: Cannot read property 'postData' of undefined
? By this, I would like to confirm it. – Tanaike"So I'm worry that you might directly run the function of doPost." Yes I do, why wouldn't you recommend that?
, the reason of your issue could be found. In your script, it seems that Web Apps is used. So please run the script with the Web Apps. developers.google.com/apps-script/guides/web – Tanaike