這是一些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網站允許按鈕點擊操作? –