1

In order to share with my colleagues the AppsScript code I am developing, I created a standalone project used as a library in our Docs template.

This library contains:

  • a .gs file for the server side code
  • a .html file for the client side sidebar

In the sidebar, I have a button which triggers a call to a function from the library with a parameter.

The javascript call is the following:

        google.script.run
            .withFailureHandler(
              function(msg, element) {
                showError(msg, $('#button-bar'));
              })
            .test();

I read on other pages that the library code wasn't exposed so the test function is in fact in the AppsScript code of my Docs and calls the equivalent library function. As it was suggested here: Can a Google Spreadsheet Apps Script library contain a user dialog?

Code in the Docs AppsScript:

function test()
{
  myLibrary.test();
}

Code in myLibrary library:

function test()
{
  DocumentApp.getUi().alert('test');
}

The problem is that the failure handler from the javascript returns a ScriptError stating that I need to have the authorization to perform this action.

Any ideas of what I am doing wrong?

PS: I know I could make an add-on but this is not something I can easily do inside my company :)

Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54

1 Answers1

2

Yes, when you use the html code (sidebar) from your library it'll call the test() function inside the script bound to the document rather that the one in the library.

You need to request permission to the user to be able to prompt UI elements, you can do this with a custom menu [1].

Also, the users need to have at least read access on the library [2]. I add the withSuccessHandler function [3] to show the response from client-side in the sidebar. The following code worked for me:

Script bound to the document:

function onOpen() {
  DocumentApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .createMenu('Custom Menu')
      .addItem('Show sidebar', 'showSidebar')
      .addToUi();
}

function showSidebar() {
  myLibrary.showSidebarLibrary();
}

//
function test() {
  myLibrary.test();
}

Library:

code.gs

function showSidebarLibrary() {
  var html = HtmlService.createHtmlOutputFromFile('Page')
      .setTitle('My custom sidebar')
      .setWidth(300);
  DocumentApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .showSidebar(html);
}

function test() {
  DocumentApp.getUi().alert('test');
  return "Success";
}

Page.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    Hello, world!<br>
    <input type="button" value="Alert" onclick="alert()" /><br>
    <input type="button" value="Close" onclick="google.script.host.close()" />
    <p id="msg">Replace message with response<p>
    </body>
    <script>
        function alert() {
            google.script.run.withFailureHandler(handler).withSuccessHandler(handler).test();
        }
        function handler(msg) {
            document.getElementById("msg").innerHTML = msg;
        }
  </script>
</html>

[1] https://developers.google.com/apps-script/guides/menus

[2] https://developers.google.com/apps-script/guides/libraries#gaining_access_to_a_library_and_including_it_in_your_project

[3] https://developers.google.com/apps-script/guides/html/communication#success_handlers

Andres Duarte
  • 3,166
  • 1
  • 7
  • 14
  • This is exactly what I did, so I tried your code and it does the same. The alert doesn't know and I get a ScriptError, you must have the permissions to execute this action. I am not doing this on standard google user account but company account, could there be some restrictions related to that ? – Clement Letonnelier Dec 12 '19 at 08:10
  • To they have access to the library script? I tested the code as I explained and it worked successfully, sharing the library with a user in my G suite domain. – Andres Duarte Dec 12 '19 at 09:09
  • I am the one testing, I use the library and the document, so I have access to both. – Clement Letonnelier Dec 12 '19 at 10:48
  • Run manually a function in both your script and library script, it should request your permissions the first time. If it doesn't work, may be share a project publicly where you can reproduce this and I'll take a look. – Andres Duarte Dec 12 '19 at 10:53
  • I already did run from both and it worked, seems there are some other rights restriction. I am afraid I can't share publicly, we are a big company, public sharing is not available. – Clement Letonnelier Dec 12 '19 at 11:39
  • 1
    It worked? If it helped please consider accepting the answer as correct. It should work if you're using the same code as me and the library version is updated in both scripts. – Andres Duarte Dec 12 '19 at 11:44
  • No no I mean calling a function from library or my script does work and doesn't ask for any permission. But then it still doesn't work. – Clement Letonnelier Dec 12 '19 at 11:48
  • 1
    Just in case, these are the steps: 1) On the document, click "Show sidebar" menu which will request the needed permissions and open the sidebar. 2) On the sidebar, click `Alert` button. 3) Click "Ok" on alert dialog. 4) The text in sidebar

    element will be changed to `success`.

    – Andres Duarte Dec 12 '19 at 11:55
  • It could be a specific issue with the file you're testing or some restriction in your domain. – Andres Duarte Dec 12 '19 at 11:56