Google Apps Scripts Libraries — How to set them up and turn Slack into a real-time logging platform

Matt “Rudy”
Maveris Labs
7 min readJan 29, 2021

--

If you have read the other blog posts related to our company’s virtual week of holiday festivities “Maveristmas” (here, here or here), then you will be fully aware that we used a lot of Google’s Apps Script to help automate and execute the events of the week. During development we noticed we were copying a lot of functions and code between different Google Apps Scripts we were creating. Additionally, we realized it would improve operations if we could have some real time logging for when forms were submitted or when errors might occur. We wanted the ability to share common functions between different Apps Script projects just like you would with a software library. And of course, as you would suspect, Google does allow you to create Apps Script Libraries. So I am going to show you how to set up an Apps Script Library by using our Slack Logging Library setup for Maveristmas as a guide.

Use Case — Logging to Slack

Our Problem:

During Maveristmas, we wanted to have some sort of logging capability and tracking to ensure things were working. For example, we wanted to have some real time solution that would let us know that someone had submitted one of our Google Forms. Additionally, we realized that Google Forms has some limited capability for validating our data submission requirements. For example, there is no built-in way to have an Apps Script conduct custom validation on submitted data, stop the form submission, and present the form back to the user. This is because the trigger for the Apps Script to validate the data occurs after the Form has been submitted and entries have been recorded. So we determined we needed some additional logging or a notification system to alert us of an error. In Allen’s post he highlights one of these error validation checks we needed.

Solution:

Google does allow for some logging in the Google Apps Script platform. However, there was no direct notification of errors and it didn’t provide some information we thought was vital. Also, no one wanted to keep refreshing a browser tab. Since we were already using Slack for communicating during the week of festivities, we decided we could log stuff to a Slack channel. We could then receive notifications in real time and allow other employees access to the channel to ensure transparency into what was occurring. I strongly suggest the reader think about how you could use Slack for logging other things as well; I know it has me thinking.

To set up the Slack logging, we created a separate Slack channel in our company workspace called maveristmas_logging. The decision was to integrate this into the Maveristmas Slack App I had already created (Other blog post), which means the App would be given authorization to post to a specific channel. If following along with a Slack App, find “Your Apps” at https://api.slack.com/apps.

Create an incoming Webhook

Create an incoming webhook, select the channel in the workspace that was created for the App to post messages in and grant permission.

Giving a Slack App Permissions to Post in a Given Channel

This will then create a webhook, which is a URL that can receive formatted POST data that will then be posted in a Slack channel. In Apps Script, the UrlFetchApp can be used. Below are two simple functions that can be used to send logs to a Slack channel. The first function is used to format JSON POST data correctly for Slack. The second is the simple function to actually post the desired message to Slack.

Example Code for Simple Logging to Slack via Webhook

Frustration:

Now you could take this and add it to any Apps Scripts that you created and, using the webhook, it would post the message to the logging channel. So there would be a lot of copy and paste or sharing slightly different functions between people working on scripts. So obviously we knew there had to be a better way, as I already preluded in this post’s introduction.

So we decided to go the route of setting up an Apps Script Library. Of note, Google clearly says that using Libraries in Apps Scripts will slow down execution. So if that is a concern with what you are developing, I recommend just embrace the frustration and keep sharing.

Set up an Apps Scripts Library:

Start by creating a new Google Apps Script project. Proceed to add the code you want to be able to use in other Apps Scripts. If you have multiple functions, create them separately within the script. After you share the library, you can call them individually as needed. In our slack logging example, I added the code from above to the project with the easy to remember function of log_to_slack.

To turn the Apps Scripts into a Library you select the New Deployment button in the editor. From there you will select the gear icon to select to deploy it as a Library. Enter a description for the Library.

Deploy script as a Library

Once deployed it will create a version of the script. Provide the Deployment ID as well as the Library URL to easily share with others.

Information provided after successfully deploying script as a Library

Before you can add the script to another Apps Script you have to know the scripts id. You can get this info from the Project Properties. There is a gear icon on the left hand side of the page. Copy this Script ID.

Project Properties to get Script ID

With the Script ID, go to the Apps Script that you want to work with. In our example this would be one of the Apps Scripts tied to one of our Maveristmas forms. While accessing the code editor of the project, there should be the word Libraries select the + sign to get a window to Add a Library.

Add a Library

Just copy in the Script ID in the provided space and select to Look up. Make sure you select the version of the Library you want to use and add an Identifier. This Identifier is what you will use when you want to reference a code in the Library.

Window to Add a Library

In the picture above you can see we made a simple Identifier for our Library called SlackLog. Once it is saved it is added as a resource for that Apps Script project. To call the log_to_slack function from within the Library, just use the Identifier, a ‘.’, and then the function with whatever parameters. For example, SlackLog.log_to_slack(“It Works”);

In our slack logging example, we wanted to log when a form was submitted.

We added the Library to the Apps Script of the form with a project trigger that executes on submit. Then we added the 4 lines of code below.

The code grabs the event object from the submission. From the event object, the email address is set to the email variable. And then we call our log_to_slack function, as mentioned above, with the desired message. The message contains the email address and the Title from the active form. Add these to different forms as needed and in the slack channel you could see output such as the image below.

Example of some logging to slack

Conclusion:

In this post we demonstrated how to set up a Library for use among different Apps Scripts. It was not mentioned in detail, but if sharing with other people, you need to ensure the correct permissions are available to do some of these actions. The blogpost showed a very simplistic case of how to use the Library to share just a small function. In reality it might be more helpful to build a Library that provided different message formatting, or took the email and form title as inputs and were formatted in a Library function, ensuring the formatting was consistent. Either way, we know that if we do a similar project again, we will use a Library more often for common functions. We also really like the use of real time logging to Slack.

Website: www.maveris.com

Email: info@maveris.com

Maveris exists to help your organization reach its fullest potential by providing thought leadership in IT and cyber so you can connect fearlessly. To learn more visit us at: www.maveris.com

--

--