Create / populate a drop-down menu on a web page from Google Spreadsheet data

I am using a Google visualization query to infer column headings from this spreadsheet . Currently, I have to include row and column indexes for every cell I want in the menu. I would like the script to populate this menu dynamically with data from the cells in row 1 of the spreadsheet. In other words, make the menu large or small than the existing one in the spreadsheet.

I believe that I need something like the following, but I'm not sure how to implement it. Maybe I need a server side script (GAS) to execute this?

<select>
     for (var i = 0; i < data.length; ++i) { 
      <option>!= data[i] </option>
     } 
  </select>

I found a lot of documentation for this using Google HTMLService, but I need this menu to be hosted using a different service.

Any help would be greatly appreciated. Thank!

Now my current code is ...

google.load('visualization', '1', {'packages':['corechart']});
	google.setOnLoadCallback(ValIDS);
    
	function ValIDS() {
	var queryValIDS = new google.visualization.Query('https://docs.google.com/spreadsheet/ccc?key=1HpHMfoEnPgESb2XPVCgb7XyGwRAvrq3EoQj4WHj4vhA&sheet=QUERY2');

	queryValIDS.send(handleQueryValIDResponse);
    }
 
 	function handleQueryValIDResponse(response) {
            if (response.isError()) {
                alert('Error in ID Validation Query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
                return;
            }

	var datatable = response.getDataTable();	
	var cat1 = datatable.getValue(0,0);
	var cat2 = datatable.getValue(0,1);
	var cat3 = datatable.getValue(0,2);
	var cat4 = datatable.getValue(0,3);
	var cat5 = datatable.getValue(0,4);
	var cat6 = datatable.getValue(0,5);
	var cat7 = datatable.getValue(0,6);
	var cat8 = datatable.getValue(0,7);
	var cat9 = datatable.getValue(0,8);
	var cat10 = datatable.getValue(0,9);
	
	
      
		document.getElementById('cat1').innerHTML = cat1;
		document.getElementById('cat2').innerHTML = cat2;
		document.getElementById('cat3').innerHTML = cat3;
		document.getElementById('cat4').innerHTML = cat4;
		document.getElementById('cat5').innerHTML = cat5;
		document.getElementById('cat6').innerHTML = cat6;
		document.getElementById('cat7').innerHTML = cat7;
		document.getElementById('cat8').innerHTML = cat8;
		document.getElementById('cat9').innerHTML = cat9;
		document.getElementById('cat10').innerHTML = cat10;
				
       
        }

function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
}

window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
.dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
    background-color: #3e8e41;
}

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    overflow: auto;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown a:hover {background-color: #f1f1f1}

.show {display:block;}

.dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
    background-color: #3e8e41;
}

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    overflow: auto;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown a:hover {background-color: #f1f1f1}

.show {display:block;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>


<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <div id="myDropdown" class="dropdown-content">
    <a href="#"><div id="cat1"></div></a>
    <a href="#"><div id="cat2"></div></a>
    <a href="#"><div id="cat3"></div></a>
	<a href="#"><div id="cat4"></div></a>
    <a href="#"><div id="cat5"></div></a>
    <a href="#"><div id="cat6"></div></a>
	<a href="#"><div id="cat7"></div></a>
    <a href="#"><div id="cat8"></div></a>
    <a href="#"><div id="cat9"></div></a>
	<a href="#"><div id="cat10"></div></a>
 
  </div>
</div>
Run codeHide result
+1
source share
1 answer

you can use client-side JavaScript to dynamically create a drop-down list using data from datatable

There are several ways, but this should do what you need.

you can use getNumberOfColumnsinstead of hard coding each drop-down list
this will allow you to add additional columns to the spreadsheet,
without having to change the / html code

for (var i = 0; i < datatable.getNumberOfColumns(); i++) {
  var ddItem = document.getElementById('myDropdown').appendChild(document.createElement('A'));
  ddItem.href = '#';
  var ddItemContent = ddItem.appendChild(document.createElement('SPAN'));
  ddItemContent.id = 'cat' + (i + 1);
  ddItemContent.innerHTML = datatable.getValue(0, i);
}

(i + 1) id ,
id ,

(<a>) ,
a <span> <div>

id, SPAN, ,

. ...

google.load('visualization', '1', {'packages':['corechart']});
google.setOnLoadCallback(ValIDS);

function ValIDS() {
  var queryValIDS = new google.visualization.Query('https://docs.google.com/spreadsheet/ccc?key=1HpHMfoEnPgESb2XPVCgb7XyGwRAvrq3EoQj4WHj4vhA&sheet=QUERY2');
  queryValIDS.send(handleQueryValIDResponse);
}

function handleQueryValIDResponse(response) {
  if (response.isError()) {
    alert('Error in ID Validation Query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
    return;
  }

  var datatable = response.getDataTable();
  for (var i = 0; i < datatable.getNumberOfColumns(); i++) {
    var ddItem = document.getElementById('myDropdown').appendChild(document.createElement('A'));
    ddItem.href = '#';
    var ddItemContent = ddItem.appendChild(document.createElement('SPAN'));
    ddItemContent.id = 'cat' + (i + 1);
    ddItemContent.innerHTML = datatable.getValue(0, i);
  }
}

function myFunction() {
  document.getElementById('myDropdown').classList.toggle('show');
}

window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    var dropdowns = document.getElementsByClassName('dropdown-content');
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
.dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
    background-color: #3e8e41;
}

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    overflow: auto;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown a:hover {background-color: #f1f1f1}

.show {display:block;}

.dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
    background-color: #3e8e41;
}

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    overflow: auto;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown a:hover {background-color: #f1f1f1}

.show {display:block;}
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="dropdown">
  <button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <div id="myDropdown" class="dropdown-content"></div>
</div>
Hide result

EDIT

getValue

getValue(rowIndex, columnIndex)

rowIndex columnIndex ,
, getValue(0, 0)

...

i rowIndex 0 columnIndex

for (var i = 0; i < datatable.getNumberOfRows(); i++) {
  var test = datatable.getValue(i, 0);
}
+2

Source: https://habr.com/ru/post/1655934/


All Articles