【发布时间】:2015-07-30 12:51:47
【问题描述】:
Google 应用脚本、javascript 等的新手...
尝试在电子表格中创建一个侧边栏,其中包含一个 html 选择对象,该对象是从其中一个工作表中的列填充的。
我尝试使用在this post 中找到的代码,但加载选项的函数似乎在加载 html 时并未执行。知道我做错了什么吗?
code.gs 中的代码:
function onOpen(e) {
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.createMenu('USD Conversion')
.addItem('Convert Dollars to Foreign Currency', 'showSidebar')
.addToUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var csvMenuEntries = [{name: "Load rates from CSV file", functionName: "importFromCSV"}];
var addCountriesMenuEntries = [{name: "Add Countries", functionName: "addCountries"}];
var conversionEntries = [{name: "Convert Dollars for Foreign Currency", functionName: "showSidebar"}];
}
/**
* Opens a sidebar in the document containing the add-on's user interface.
*/
function showSidebar() {
var template = HtmlService
.createTemplateFromFile('Sidebar')
var htmlOutput = template.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('USD Conversion')
.setWidth(400);
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.showSidebar(htmlOutput);
}
function getListOptions() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("CountryCodes");
var lastRow = sheet.getLastRow();
var myRange = sheet.getRange("P2:P"+lastRow);
var countries = myRange.getValues();
return( countries );
}
Sidebar.html 中的代码:
<div class="labels">
<p>Destination Country:</p>
</div>
<div>
<select name="optionList">
<option>Loading...</option>
</select>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
// The code in this function runs when the page is loaded.
$(function () {
google.script.run.withSuccessHandler(buildOptionList)
.getListOptions();
});
function buildOptionList(countries) {
var list = $('#optionList');
list.empty();
for (var i = 0; i < countries.length; i++) {
list.append(countries[i]);
}
}
</script>
【问题讨论】:
标签: javascript jquery html google-apps-script