这是一些JavaScript代码加载选项到选择框中。
function updateSelect(vA)//array of values that go into the drop down or select box
{
var select = document.getElementById("sel1");
select.options.length = 0;
for(var i=0;i<vA.length;i++)
{
select.options[i] = new Option(vA[i],vA[i]);
}
}
这是选择框的html。
<select id="sel1" style="width:125px;height:35px;margin:10px 0 10px 0;">
<option value="" selected></option>
</select>
对我来说看起来很简单。
这是一个更完整的解决方案。
代码:
function getSelectOptions()
{
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Options');
var rg=sh.getDataRange();
var vA=rg.getValues();
var options=[];
for(var i=0;i<vA.length;i++)
{
options.push(vA[i][0]);
}
return vA;
}
function showSidebar()
{
var userInterface=HtmlService.createHtmlOutputFromFile('f');
SpreadsheetApp.getUi().showModelessDialog(userInterface, 'The Drop Down with No Options now has options.');
}
f.html
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(function() {
$('#txt1').val('');
google.script.run
.withSuccessHandler(updateSelect)
.getSelectOptions();
});
function updateSelect(vA)
{
var select = document.getElementById("sel1");
select.options.length = 0;
for(var i=0;i<vA.length;i++)
{
select.options[i] = new Option(vA[i],vA[i]);
}
}
console.log("My code");
</script>
</head>
<body>
<select id="sel1" style="width:125px;height:35px;margin:10px 0 10px 0;">
</select>
</body>
</html>
这里是片命名选项
谁告诉脚本是不可能在谷歌的网站? – Ritz
您可以创建一个作为Web应用程序发布的Google Apps脚本。然后您使用iFrame显示发布的网络应用程序。请注意,新网站目前可能不支持此功能。在这种情况下,你会链接并进入一个新的页面。由于Web应用程序可以具有HTML和CSS,因此您可以使其看起来像是该网站中的另一个页面。 –
@Ritz:哦!所以我该如何使用脚本进行动态更改。 Google网站允许按钮点击操作? –