根据
https://developers.google.com/apps-script/guides/html/communication#private_functions
script无法访问库函数,但看起来它也无法访问库对象。
解决方案是使用全局变量
代码.gs
/* For tests only, assign the server side function
* to be called from the sidebar to a global variable
*/
var email = getEmail();
/**
* Reference: https://stackoverflow.com/q/50595103/1595451
*
*/
function onOpen(e) {
var ui = SpreadsheetApp.getUi();
var menu = ui.createMenu('Sidebar');
menu
.addItem('Open', 'showSidebar')
.addToUi();
}
function showSidebar() {
var ui = HtmlService.createHtmlOutputFromFile('Sidebar')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('A Sidebar');
SpreadsheetApp.getUi().showSidebar(ui);
}
function getEmail() {
return Session.getActiveUser().getEmail();
}
用作库客户端的电子表格
代码.gs
var email = aLib.email;
function onOpen(e) {
aLib.onOpen(e);
}
function showSidebar(){
aLib.showSidebar();
}
function getEmail(){
/* Instead of calling the library function we call the library global variable */
return email;
}