How can I reference a Google Apps Script .html file from another library?

自闭症网瘾萝莉.ら 提交于 2020-06-28 11:03:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!