witham
witham

Reputation: 149

Google html creating a list from spreadsheet

I am trying to get a list to populate from a spreadsheet based on the question posted here Previous questioe posted by @Kron011and I'm struggling somewhat. I added these lines to my .gs file:

function getMenuListOne(){
return SpreadsheetApp.openbyId('spreadsheet_key').getSheetByName('sheet1')
.getRange(row, column, numRows, numColumns).getValues();
}

and I added these lines to my HTML file:

  <select id="menu">
  <option></option>
  <option>Google Chrome</option>
  <option>Firefox</option>
  </select>


<script
src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
// The code in this function runs when the page is loaded.
$(function() {
  google.script.run.withSuccessHandler(showThings)
      .getMenuListFromSheet();
  google.script.run.withSuccessHandler(showMenu)
      .getMenuListFromSheet();
});

function showThings(things) {
  var list = $('#things');
  list.empty();
  for (var i = 0; i < things.length; i++) {
    list.append('<li>' + things[i] + '</li>');
  }
}

function showMenu(menuItems) {
  var list = $('#menu');
  list.find('option').remove();  // remove existing contents

  for (var i = 0; i < menuItems.length; i++) {
    list.append('<option>' + menuItems[i] + '</option>');
  }
}

</script>

As ever my painfully limited experience is hampering my efforts. I can get a new menu box to appear and show the correct results I want but I cannot get an existing box to show the same list. The existing boxes code is currently:

<input type="text" name="site" list="depotslist" id="site" class="form-control" placeholder="Select depot/site" required>
                    <datalist id="depotslist">
                    <option value="one">
                    <option value="two">
                    </datalist>

but can someone please point me in the right direction of which parts of my existing menu box I need to change to get the two bits to communicate?

UPDATE 23 JULY

I added the following code to get the another list to operate from another source:

    $(function() {
  google.script.run.withSuccessHandler(showThings2)
      .getMenuListSources();
  google.script.run.withSuccessHandler(showMenu2)
      .getMenuListSources();
});

function showThings2(things2) {
  var list2 = $('#things2');
  list.empty();
  for (var i = 0; i < things2.length; i++) {
    list2.append('<li>' + things2[i] + '</li>');
  }
}

function showMenu2(menuItems2) {
  var list2 = $('#menu2');
  var box2 = $('#sourcelist');
  list2.find('option').remove();  // remove existing contents

  for (var i = 0; i < menuItems2.length; i++) {
    list2.append('<option>' + menuItems2[i] + '</option>');
    box2.append('<option>' + menuItems2[i] + '</option>');
  }
}

with these lines in the .gs file:

    var Bvals = SpreadsheetApp.openById(ssKey).getSheetByName('SourceCodes').getRange("C3:C").getValues();
var Blast = Avals.filter(String).length;
return SpreadsheetApp.openById(ssKey).getSheetByName('SourceCodes').getRange(3,3,Blast,1).getValues();

Upvotes: 0

Views: 873

Answers (1)

Gerardo
Gerardo

Reputation: 3845

It would be similar to what you are doing with the element "menu". with jquery you can select the element of the box that contains the options and then append the new values. in this case it's id is: depotslist.

function showMenu(menuItems) {
  var list = $('#menu');
  var box =  $('#depotslist');
  list.find('option').remove();  // remove existing contents

  for (var i = 0; i < menuItems.length; i++) {
    list.append('<option>' + menuItems[i] + '</option>');
    box.append('<option>' + menuItems[i] + '</option>');

  }

As a difference with the element "menu" in this case the content will remain. This means that in the box you will see the options "one, two" and whatever you added because we are not removing the content like with this line

list.find('option').remove();

I'm not sure if this is exactly what you wanted to do, let me know if this doesn't help.

Upvotes: 1

Related Questions