这是对Html 服务:最佳实践文档中演示的技术的改编。
代码.gs
function doGet() {
var template = HtmlService
.createTemplateFromFile('DynamicList');
var htmlOutput = template.evaluate()
.setSandboxMode(HtmlService.SandboxMode.NATIVE);
return htmlOutput;
}
function getListOptions() {
// In production code, get an array of options by
// reading a spreadsheet.
var options = ['Saab','Opel','Audi'];
return( options );
}
动态列表.html
<div>
<select id="optionList">
<option>Loading...</option>
</select>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
// This code in this function runs when the page is loaded.
$(function() {
google.script.run.withSuccessHandler(buildOptionList)
.getListOptions();
});
function buildOptionList(options) {
var list = $('#optionList');
list.empty();
for (var i = 0; i < options.length; i++) {
list.append('<option value="' + options[i].toLowerCase() + '">' + options[i] + '</option>');
}
}
</script>