2

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 Answer 1

3

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();
}
Sign up to request clarification or add additional context in comments.

4 Comments

@Kriggs This works great - thanks! But I'm now unable to use google.script.run from those pages to call back to the server. This link also seems to say that I can't those server functions within libraries.
@GilbertW Can you post a snippet of what you're trying to accomplish?
I updated the page.html example above to call myFunc() in the Library, but cannot get "Hallo Werld!" to be logged. It will work if put myFunc() in the Script Editor, but that still kind of defeats the purpose of the library.
You'll have to use a workaround, make another function, function myLibsFunction( functionToCall ){ myLibs[ functionToCall ]() }, then use myLibsFunction('myFunc'), this myLibsFunction must be in every spreadsheet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.