Google Doc Add-on

80 views
Skip to first unread message

Edward Ulle

unread,
Jun 12, 2020, 10:13:14 AM6/12/20
to Google Apps Script Community
The Google Add-on pages are not descriptive enough to figure out how to make an add-on.

I am thinking of developing an add-on for Google Docs.  And am at a point where I am trying to figure out how to have all my code, html, css, javascript in a stand alone library.  The html would be for a sidebar and other user interface dialogs.  My main question is how do I use google.script.run to access a method in my library rather than the code.gs page of the document?

I envision my add-on as having only one function in the document to display the sidebar.  But depending on the position of the cursor whether to interact with the document such as cursor position or with the sidebar or any other dialog the sidebar triggers.

Also the Google Add-on pages says I need to use Cards but that page says its limited to Gmail.

TIA

Ed Bacher

unread,
Jun 12, 2020, 11:14:12 AM6/12/20
to Google Apps Script Community
Hi Edward,

I'm not sure what you want your Docs add-on to do, but I can point you to the code for a working Docs add-on. I created Docs to Markdown, and I recently open-sourced it: you can find the code and installation information here: https://github.com/evbacher/gd2md-html.

As you suggest, the HTML for the client-side UI would be in addon/sidebar.html. The rest of the code is in the addon/*.gs files and runs on the server.

To answer your question about google.script.run, you'll need to call that from the sidebar (you can see two examples in my code, one for Markdown, one for HTML). Here's that bit:

    // Call the main add-on function (convertX()) on the server.
    // We also send the names of callback functions to call in case of
    // success or error.
    google.script.run
      .withSuccessHandler(displayServerMessage)
      .withFailureHandler(displayServerError)
      .convertToMarkdown(config);

The code in addon.gs just sets up the add-on, displays the sidebar and then passes off the calls from sidebar to the implementation code.

Let me know if you have any other questions. I have written a very basic hello-world Docs add-on, but I have not open-sourced it yet. I'll try to get on that soon.

Best,
- Ed

Alan Wells

unread,
Jun 12, 2020, 12:42:38 PM6/12/20
to Google Apps Script Community
Google add-ons are in two basic categories:

1) Editor Addons
2) GSuite Addons

As usual, it might not exactly be the best terminology.  For example, both types of add-ons can be used in GSuite.
Editor Addons are for.
Docs
Sheets
Forms
Slides

GSuite Addons are for:
Gmail
Calendar
Drive

Editor Addons use the HTML interface.
GSuite Addons use the Card Service interface.

Editor add-ons came first, then GMail add-ons.
GMail add-ons came before Calendar and Drive add-ons.

In my opinion, the documentation hasn't really totally integrated with Calendar and Drive add-ons.
The documentation seems to be constructed with Calendar and Drive add-ons as an afterthought.
Like the addition that you just kind of throw onto the end of your house without much planning.

A simple onOpen function (trigger) can only run for 30 seconds.  So, keep that in mind.

I believe, that you would need two Apps Script projects for the add-on if you are going to use a library in the add-on.
The script published as the add-on will need the library added to it.
I'm not sure if there's any advantage at all to that.
The only advantage that I can think of, is if there is a way to update the library in the add-on project without republishing the add-on.
Which in affect would update your add-on without the need to republish the add-on.
But other than that, I can't think of any reason to use the library in the add-on project.

I think that you can publish the project as BOTH an add-on and a library, so people could use it either way.
But they couldn't use the library without doing some coding on their own to access the library.

Ed Bacher

unread,
Jun 12, 2020, 1:16:29 PM6/12/20
to Google Apps Script Community
Ahh, good point about using libraries. The first version my Docs add-on did have the bulk of the implementation in the library script. Then the add-on was the sidebar and setup stuff in addon.gs. It was a pain maintaining two separate scripts, though as you point out, you can update the library without publishing a new version of the add-on, so that's a consideration.

I know that the Apps Script folks at one time advised against using libraries since they could be slow, but I did not run into any problems with that (though I'm not using a library now, so can't verify if anything has changed).
- Ed

Edward Ulle

unread,
Jun 14, 2020, 9:38:08 AM6/14/20
to Google Apps Script Community
I'm still having difficulty with arranging my code into libraries, add-on and server.  As a stand alone bound script I am able to determine if my cursor is over the sidebar or the doc, all google.script.runs work.  But when I move the sidebar to the add-on or library my server calls fail.  I move the sidebar and all its javascript to the add-on now when I call google.script.run the responsiveness is lost.  In other words the code associated with doSomething in google.script.run.doSomething was in Code.gs, now its in the library.

Edward Ulle

unread,
Jun 15, 2020, 5:07:58 PM6/15/20
to Google Apps Script Community
The only way I can see to communicate with the server is to have a wrapper function installed in the Doc Code.gs.  Can someone point me to how to install this..

In Code.gs:

function doSomething(request) {
  return MyLibrary.runRequest(request)
}

In MyLibrary.gs:

// some where in MyLibrary
google.script.run.withSuccessHandler( function (result) { ... }).doSomething(request);

function runRequest(request) {
  // uppack request to see what function to run
  if( request.name === "doFunction1" }
    return doFunction1(request.data);
  else if( request.name === "doFunction2" ) {
    return doFunction2(request.data);
  }
}

Andrew Roberts

unread,
Jun 16, 2020, 5:06:46 AM6/16/20
to google-apps-sc...@googlegroups.com
You are right that if the main body of the code is in a library, for client-side calls to work the function has to be in the script calling the library.

Now you'd deploy the add-on from the script calling the library, rather than from the library script.

That's what I do for the add-on "Rose Task Manager". The add-on is deployed from this small script which makes all it's calls to the main library which does various client-side calls.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/34d3dc74-1530-412d-948b-b9dbf95a99e2o%40googlegroups.com.

Edward Ulle

unread,
Jun 16, 2020, 9:25:06 AM6/16/20
to Google Apps Script Community
andrew,

thanks, but again how do the addon scripts get installed (copyied) to the server script file Code.gs?

Ed
Reply all
Reply to author
Forward
0 new messages