Vasim Vasim - 27 days ago 25
HTML Question

HTML select option to be captured from Google Spreadsheet

I have been spending days to get over this but no luck.

I have a html page in Google script whereby I want a dropdown list - this is achieved as follows

<div>
<select name="source">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
</div>


Now instead of the manual inputs I want the option fields to be captured from a spreadsheet, if the coloumn range increase or decrease the dropdown list should update accordingly.

Any workarounds please...

Regards,

Answer

To display the elements in the array instead of the array object itself, you need to include the DATA array inside Scriptlet tags.And I see no requirement to use a 2-dimensional array. The correct syntax should be:

<?
 var sheet   =  SpreadsheetApp.openById(0Avt7ejriwlxudGZfV2xJUGJZLXktQ2RhQU1ugtgtaXc").getSheetByName("MRF Tab");
  var lastRow = sheet.getLastRow();  
  var myRange = sheet.getRange("C3:C"+lastRow); 
  var data    = myRange.getValues();
  ?>
<div>
  <select>
    <? for (var i = 0; i < data.length; ++i) { ?>
      <option><?!= data[i] ?></option>
    <? } ?>
  </select>
</div>

And its really a bad idea to mix data-fetching code & html UI code within a template. You should use separate gs and html files & apply the optimizations as Mogsdad has suggested.

Comments