Gilbert W
Gilbert W

Reputation: 113

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

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);
}

Upvotes: 2

Views: 3060

Answers (1)

Kriggs
Kriggs

Reputation: 3778

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();
}

Upvotes: 3

Related Questions