0

Hi I am trying to get a dropdown list using HTML and Google Script. The options should be taken from the Google Spreadsheet. Unfortunately, my list is empty all the time. The options don't appear. Anyone can help?

CODE GS

  function getListCars(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var carsheet = ss.getSheetByName("Rejestraut");
  var lastrow = carsheet.getLastRow();
  return carsheet.getRange(2,2,lastrow-1,1);
}

HTML

     ...<script>
function loadCars(){
  google.script.run.withSuccessHandler(function(ar)

  {
    var carsSelect = document.getElementById("numeryrejestracyjne");
    console.log(ar);

    let option = document.createElement("option");
    option.value="";
    option.text = "";
    carsSelect.appendChild(option);

    ar.forEach(function(item, index)
    {
      let option = document.createElement("option");
      option.value = item[1];
      option.text = item[1];
      carsSelect.appendChild("option");
    });
  }).getListCars();
};
</script>

<select id="numeryrejestracyjne">
            </select>
            <script>loadCars();</script>

2 Answers2

1

Answer

getRange does not return values, it returns a range. In order to return the values of a range, use getValues.

Example

var range = SpreadsheetApp.getActiveSheet().getRange(2, 3, 6, 4)
var values = range.getValues()

References:

fullfine
  • 1,371
  • 1
  • 4
  • 11
0

carsheet.getRange(2,2,lastrow-1,1); does not return any values it just returns a range. You should have been able to see that with your console.log(ar) in the withSuccessHandler().

Cooper
  • 59,616
  • 6
  • 23
  • 54