Adamsonss1112
Adamsonss1112

Reputation: 1

A drop-down list of cells located in the Google Sheets in HTML

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>

Upvotes: 0

Views: 319

Answers (2)

fullfine
fullfine

Reputation: 1461

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:

Upvotes: 1

Cooper
Cooper

Reputation: 64100

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().

Upvotes: 0

Related Questions