Ability to select an HTML option from a Google spreadsheet

I spent days to overcome this, but no luck.

I have an html page in google script in which 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 manual inputs, I want the parameter fields to be captured from the spreadsheet, if the coloumn range increases or decreases, the drop-down list should be updated accordingly.

Any workarounds please ...

Hi,

+1
source share
3 answers

To display elements in an array instead of the array object itself, you need to include the DATA array in the Scriptlet tags. And I do not see the need to use a two-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>

HTML- html . gs html Mogsdad.

+2

, , .

+2

, Html Service: Best Practices.

Code.gs

function doGet() {
  var template = HtmlService
                 .createTemplateFromFile('DynamicList');

  var htmlOutput = template.evaluate()
                   .setSandboxMode(HtmlService.SandboxMode.NATIVE);

  return htmlOutput;
}

function getListOptions() {
  // In production code, get an array of options by
  // reading a spreadsheet.
  var options = ['Saab','Opel','Audi'];

  return( options );
}

DynamicList.html

<div>
  <select id="optionList">
    <option>Loading...</option>    
  </select>

</div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
// This code in this function runs when the page is loaded.
$(function() {
  google.script.run.withSuccessHandler(buildOptionList)
      .getListOptions();
});

function buildOptionList(options) {
  var list = $('#optionList');
  list.empty();
  for (var i = 0; i < options.length; i++) {
    list.append('<option value="' + options[i].toLowerCase() + '">' + options[i] + '</option>');
  }
}
</script>
+2

All Articles