Google Sheets web app register several options

隐身守侯 提交于 2019-12-24 18:11:35

问题


I've taken an example from a book to adapt it. the thing is that the original code has radio check (to enable only one option) and I'd like to have them as checkbocxes (to register several choices). I've made the changes in the HTML code and it looks fine but the spreadsheet still registers one of the choices instead of all of them. Could you please let me know what I'm missing?

Here's the HTML of the project

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <form method="post" action="<?= pubUrl ?>">
 <h4>Where will you go for vacation?</h4>
 <? for (var i in places) { ?>
 <input type="checkbox" name="places"
 value="<?= places[i] ?>" /><?= places[i] ?><br />
 <? } ?>
 <br />
 <input type="submit" value="SUBMIT" />

 </form>

  </body>
</html>

And here's the GAS (google Script)

function doGet() {
 // Replace with your spreadsheet's ID.
 var ss = SpreadsheetApp.openById("1hpYbBbpVfxsciVCpZGBcqHPeBo2Wuj7U1Y9CaxsI9go");
 var SheetPlaces = ss.getSheetByName("Places");

 var data = SheetPlaces.getDataRange().getValues();

 // Remove header row.
 data.shift();

 var places = [];

 // Populate the places array with the first column's data.
 data.forEach(function(row){
 places.push(row[0]);
 });

 var template = HtmlService.createTemplateFromFile("form.html");
  
 //Assign the published URL to the template object in the doGet 
template.pubUrl =  "https://script.google.com/macros/s/AKfycbx90heH12wfP4-kVZCOkEpI7Bi5wqpDAf-ndLrf3bPPYSwwEp5Q/exec";
  //o
  //template.pubUrl = ScriptApp.getService().getUrl();
 // Assign the places array to the template object.
 template.places = places;
 var html = template.evaluate();
 return HtmlService.createHtmlOutput(html);
}

function doPost(e){  
  // Replace with your spreadsheet's ID.  
  var ss = SpreadsheetApp.openById("1hpYbBbpVfxsciVCpZGBcqHPeBo2Wuj7U1Y9CaxsI9go");
  var SheetResponses = ss.getSheetByName("Responses");   
  // Create a 'Responses' sheet if it does not exist.  
  if(!SheetResponses){     SheetResponses = ss.insertSheet("Responses");
   };    SheetResponses.appendRow([e.parameter.places]);    return ContentService.createTextOutput(     "Your response submitted successfully. Thank you!"  );
}


function createForm() {

 var ThisSpreadsheet = SpreadsheetApp.getActive();
 var SheetPlaces = ThisSpreadsheet.getSheetByName("Places");
  
  
 // Load 'Places' sheet data as a 2-dimensional array.
 var data = SheetPlaces.getDataRange().getValues();

 // remove the header row
 data.shift();

 var places = [];

 // Populate the places array with the first column's data
 data.forEach(function(row){
 places.push(row[0]);
 });

 // Create a new form
 var form = FormApp.create("Vacation Form");

 form.addMultipleChoiceItem()
 .setTitle('Where will you go for a vacation?')
 .setChoiceValues(places)
 .showOtherOption(true)

}

Also, here's the web app url https://script.google.com/macros/s/AKfycbx90heH12wfP4-kVZCOkEpI7Bi5wqpDAf-ndLrf3bPPYSwwEp5Q/exec

Thanks for your help!

来源:https://stackoverflow.com/questions/55171161/google-sheets-web-app-register-several-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!