问题
I'm writing a set of HTML pages that will be served on the Google Sheets sidebars and will connect to our internal database. Each page is set up on its own .html file within a single Google Apps Script project.
I'd like to pull these files out to a single project and reference this as a library like I can do with my other .gs script files. Specifically, how can I write the "MyLib.page" line below?
Library project:
Code.gs
function myFunc() { Logger.log("Hallo Werld!");}
page.html
<h1>
Hello world!
</h1>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
google.script.run.myFunc();
});
</script>
Spreadsheet Script Editor (Included Library Identifier: MyLib)
Code.gs
function callLibFunc() {
MyLib.myFunc();
}
function loadSidebar() {
var html = HtmlService.createTemplateFromFile("MyLib.page").evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
SpreadsheetApp.getUi().showSidebar(html);
}
回答1:
Make the Lib open the Sidebar:
Library project:
Code.gs
function loadSidebar() {
var html = HtmlService.createTemplateFromFile("MyLib.page").evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
SpreadsheetApp.getUi().showSidebar(html);
}
Spreadsheet Script Editor
Code.gs
function callLibFunc() {
MyLib.loadSidebar();
}
来源:https://stackoverflow.com/questions/31034303/how-can-i-reference-a-google-apps-script-html-file-from-another-library