使用Google Apps Script从Google Sheets生成HTML下拉列表侧边栏不显示如何解决
错误点梳理
- JS语法错误:
withSuccessHandler的回调函数括号位置错放,console.log('in loadcars')写在了回调函数作用域外,导致回调逻辑没有被绑定执行,下拉列表无法拿到后端返回的车辆数据。 - HTML属性错误:显示选中值的span标签id写法错误,原代码为
<span id>"carValue"</span>,引号位置错误导致无法通过id选中该元素。 - HTML结构不规范:
<script>标签、<h1>标签放置位置不符合HTML规范,可能导致页面渲染异常。
修正后代码
你的Code.gs逻辑完全正常,无需修改,只需替换dropdownList.html的全部内容即可:
<!DOCTYPE html> <html> <head> <base target="_top"> </head> <body> <h1>something</h1> <select id="cars" onChange="onSelect()" ></select><br> <span id="carValue"></span> <script> function loadCars() { console.log('in loadcars') google.script.run.withSuccessHandler(function(ar) { console.log(ar) var carsSelect = document.getElementById("cars"); 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[0]; carsSelect.appendChild(option); }); }).getList(); }; function onSelect() { var carID = document.getElementById("cars").value; document.getElementById("carValue").innerHTML = carID; }; loadCars(); </script> </body> </html>
验证步骤
保存修改后刷新Google Sheets页面,重新从「Custom」菜单打开侧边栏即可看到正常加载的下拉列表,选中选项后会在下方显示对应车辆ID。
内容的提问来源于stack exchange,提问作者sk65plus
相关产品推荐
相关产品推荐

