Content generation with Google Workspace Add-ons

Back when G Suite, err… Google Workspace Add-ons launched, support for Docs, Sheets, and Slides was conspicuously absent. Sure, they’ve long supported their own flavor of add-ons, but the idea of building an add-on two different ways didn’t sit well with me. Thankfully I didn’t have to wait too long for the editors to catch up. I know it felt like an eternity, but let’s just chalk that up to 2020 being what it is.

Now that add-ons work in the editors I had the chance to build a few small demos to try them out. The two samples I built both fall into the category of content generation. One inserts text, the other images. There are lots of potential add-ons that do exactly this — inserting generated charts or diagrams for a report or sales pitch, grabbing images from a stock photography catalog for a slide deck, adding canned snippets of text for an RFP, or using AI-driven text generation and summarization to accelerate writing.

As fun as building some of those might be, I wanted to build something simple that shows what could be done without going down too deep a hole. For text, what better way to show inserting text than the classic Lorem Ipsum! Ok ok ok… To be fair, Pirate Ipsum, Zombie Ipsum, Cat Ipsum or one of the many other variants would be better, but I’m sticking with the classics for this one.

For images, I went with something more useful — QR Codes. They’re simple to generate and there are plenty of good reasons for adding them to a document or presentation, even emails. But even if you don’t care about random placeholder text or QR codes, it’s just a small jump from these to inserting whatever text or image content matters to you.

Let’s jump into how they’re built.

Enable the editors in the Apps Script manifest

All add-ons start with a manifest that describes the features and entry points. For the editors, only the homepageTrigger entry points are supported. Since the home page runs in the context of an active editing session, there’s naturally more context available than in other hosts — the document itself.

As an aside, there’s a bit of duplication here and I could just have easily used a single homepage trigger for the entire add-on:

But since these two add-ons only work contextually, I thought it best to define the entry points on a per-host basis.

Request permissions

The other important part is listing the scopes the add-on needs.

In this case the add-on needs access to update the documents. Gmail needs some special scopes unique to compose-time add-ons as well. Lastly, the external_request scope allows calling the underlying web APIs that generate the lorem ipsum text and QR codes. I’m lazy and even with the new V8 runtime, it’s often a chore trying to get Node.js packages to run in Apps Script. Yes, they’re both JavaScript and both use V8, but the environments are very different. Calling an API with UrlFetchApp is much easier, so and to the rescue!

There’s more to the manifest, but it’s mostly cosmetic details. You can view the full version for both add-ons on GitHub.

Display the home card

Both add-ons require the user to enter some information and confirm inserting content — how much text to generate or the data to encode in the QR code. They’re both very similar, so let’s just take a look at one of them — the lorem ipsum generator

On submitting the form, the action callback is called.

Again, the QR code version is very similar, just a different set of options for the data to encode and requested image size.

Insert text and images

Here’s where things get a little tricky. Inserting content varies a lot depending on the host application. The add-on event indicates which application is the host for the add-on and the add-on can switch implementations on that value. Inserting text in any individual host isn’t a lot of code, but to be honest, having to write so many variations of nearly identical code bothers me. There certainly are use cases where I’d want the ability to precisely control the behavior for each host, but for this one? Nope, rather have some little helpers do the work for me.

For text insertion, it ends up looking like this.

Images are very similar:

In both cases, Gmail is a bit of an outlier. Interaction isn’t via the built-in APIs in Apps Script, but rather through returning a response from the action instructing the host app to do the work for us.

Similarly, while the other hosts expect the actual image content as a Blob, Gmail requires an HTML <img> tag. Compose-time actions in Gmail don’t support adding attachments and data URIs aren’t allowed in images either. This means the image must be served from a publicly accessible URL. That’s not a problem in this case since the QR code API can be used as the source for the image, but it’s not hard to imagine cases where add-ons will need to host their generated images in a GCS bucket or elsewhere.

Watch out for icebergs

Easy, right? Sort of.

Yes, the implementations here are straightforward. With the exception of Gmail which contains a tiny bit of HTML, this is all plain text and single images. Inserting richly formatted text is more complicated, particularly when the text is already in a format like HTML or markdown. That means parsing and transforming the markup into the right set of API calls for the host app.

Similarly, replacing text that is selected text is slightly different than inserting text at the cursor. It’s doable, just another use case to be aware of when building and testing an add-on. Neither of these two add-ons demanded that as a feature, but yours might.

Next up

While inserting content covers a broad range of add-ons for editors, it’s just part of the overall landscape. Consuming and transforming content are also powerful patterns that fit a wide variety of use cases — readability scoring, toxicity or tone analysis, data loss prevention, publishing, and so on. Stay tuned for more, and in the meantime learn more about how to build an add-on here.

Developer Relations @ Google, dad, poker enthusiast, politics junky, squirrel admirer, and more :) The opinions stated here are my own, not those of my company.