【发布时间】:2017-08-30 14:56:41
【问题描述】:
您好,我在 Google Sheet 上使用 Google App Scripts,我想组织我的代码,所以我尝试封装一些函数。但是,我似乎无法弄清楚如何从 html 模板中调用封装的函数。
有谁知道是否可以使用 google.script.run 从 html 模板调用封装函数?
例子:
代码.gs
function onOpen(e){
var menu = SpreadsheetApp.getUi().createMenu('Document Routing')
.addItem('View/Edit My Routes', 'ViewMyRoutes')
.addToUi();
}
function ViewMyRoutes() {
var html = HtmlService.createHtmlOutputFromFile('MyRoutes')
.setTitle('My Routes')
.setWidth(300);
SpreadsheetApp.getUi()
.showSidebar(html);
}
用户.gs
var User = function(){
return {
GetMyRoutes: function (){
var userProperties = PropertiesService.getUserProperties();
var routeData = userProperties.getProperty("SpreadsheetRoutes");
return routeData;
}
};
}();
示例.html
<h1>Edit Route</h1>
<select id="EditRoute" onchange="LoadSelectedRoute()"></select>
<div id='divRoutes'></div>
<script>
function LoadEditDropDown(){
google.script.run
.withFailureHandler(onFailure)
.withSuccessHandler(OnGetRoutesSuccess)
.User.GetMyRoutes();
function OnGetRoutesSuccess(scriptProperties){
//do stuff with scriptProperties here
}
}
LoadEditDropDown();
</script>
【问题讨论】: