I have a list in google sheet to be selected as option in an html. The selection was set to multiple. How can I get the selected values be placed in a single cell in google sheet?
here is the link for the google sheet:
https://docs.google.com/spreadsheets/d/1lEfzjG1zzJVPMN8r-OpeZm6q9_IqSwk9DNCEY-q7ozY/edit?usp=sharing
here is my html code for multiple:
<div class="input-field col s4">
<select id="posApp" multiple="multiple">
<option value="" disabled>Choose your preferred app</option>
<?!= list2; ?>
</select>
<label>Select App</label>
</div> <!-- CLOSE TIME FIELD -->
</div> <!-- CLOSE ROW -->
for google script (to make a selection list from google sheet):
function doGet(e){
var ss = SpreadsheetApp.openByUrl(url);
var ws2 = ss.getSheetByName("Pos_App");
var list2 = ws2.getRange(1,1,ws2.getRange("A1").getDataRegion().getLastRow(),1).getValues();
var htmlListArray2 = list2.map(function(r){return '<option>' + r[0] + '</option>'; }).join('');
var tmp = HtmlService.createTemplateFromFile("page");
tmp.list2 = htmlListArray2;
return tmp.evaluate();
}
for javascript using initialization of Select from Materialize CSS and the code from @Cooper:
<script>
document.addEventListener('DOMContentLoaded', function() {
var timeSelect = document.querySelectorAll('select');
M.FormSelect.init(timeSelect);
document.getElementById("posApp").addEventListener("change",selectMulti);
function selectMulti(){
google.script.run.withSuccessHandler(updateSelect).getSelectOptions();
}
function updateSelect(vA){
var select = document.getElementById("posapp");
for(var i=0;i<vA.length;i++)
{
select.options[i] = new Option(vA[i],vA[i]);
}
}
});
</script>
then additional function in google script also from @Cooper:
function getSelectOptions()
{
var ss = SpreadsheetApp.openByUrl(url);
var ws = ss.getSheetByName("Open_Pos");
var posRg = ws.getRange(1, 1, ws.getLastRow(), 1).getValues().join().split(",");
//return rg.getValue().split(',');
return posRg;
}
But unfortunately, it is still unsuccessful. Though the output in the HTML are all the selected data, when I click the submit button, only the 1st item in the selection were saved in the google sheet. I also don't know how to use .getSelectedValues(); from MaterializeCSS. Thank you in advance your your help.