Automation: Push Data From Spreadsheets to Slack Real Time
By: Mars Escobin
Tags: Data, Slack, Programming, Automation, API

If you’re like me, then you use Spreadsheet as a database. Yes, I said it. And no, no one will judge here. This is a safe place for us heathens. After all, who needs a full blown database for projects that will either die or evolve right?

The problem with data, regardless where they’re stored, is that they’re only useful if they’re visible. And pushing data in front of people can be a challenge sometimes. Good thing they’re easy to do when the people are in Slack and the data is in Spreadsheets!

Here are some other use cases:

You have a sensitive budget sheet and you want to get alerted whenever someone is changing a formula

You have a feedback form and you want to instantly feed the responses to your team

You have a sign up form and you want to call leads as they come in

You have a high stake proposal and you want to see who is viewing it

You’re collecting and computing data and you want to get pinged when it reaches a certain value

By the way, here’s a walkthrough of this tutorial if you’re a visual learner:

Decision Points:

Before we start, there are four decisions to make. Let’s get these out of the way:

Name of your Slack application — The first step is to to create a Slack application that can post messages whenever a trigger happens in Spreadsheets. It’s exactly like a bot, you have to name it and can give it a profile photo and a bio. You can change these anytime. Trigger — A trigger is an event of your that happens in a Spreadsheet. This can be anything. For example, a new row being filled up, a change in cell value, the opening of the sheet or even the changing of time (i.e., triggers can be scheduled). You choose what type of event you want to be a trigger. Message — The idea is to post a message on Slack when a trigger happens. What message would you like to send? Would you like to send over the data from Sheets? Or maybe craft conditional messages for specific scenarios (e.g if value exceeds a certain amount, you send a different message)? Channel — Where should these messages go? You can also have them sent over through DMs.

For the this tutorial, I’ll be creating a simple bot that will record changes in attendance status. Any change in value will be sent over to a Slack channel.

Step 1: Create your own app in Slack

Make sure you’re signed into a Slack workspace Go to https://api.slack.com/apps Tap the Create New App button Give your application a name and choose the workspace you’d like to connect it to Turn on the Activate Incoming Webhooks toggle In the Add features and functionality section, choose Incoming Webhooks Scroll down the page to add a webhook to your workspace You’d be redirected to a page where you’d be asked to choose a channel to attach the webhook to. This means the messages the slack app will send will be posted in this channel. You’d be redirected back to the Incoming webhooks page, scroll down and you will see your webhook key! Every time you add your app to a workspace you’d be given a new webhook key.

Step 2: Integrate Sheets and Slack using a webhook

Now that we have a webhook key, we can now write a script to connect our Sheet to our chosen Slack channel. This sounds deceivingly complex but is surprisingly straightforward:

Pull up your sheet’s Script Editor Store the webhook key in a variable (I’ll name mine as url) Create a function that will run when a trigger happens. We will discuss triggers more in a little bit. For now, there are only two things to note:

We need to pass a parameter, conventionally named as “e”, to denote an e vent (such as opening a sheet or editing a cell) that serves as a trigger.

vent (such as opening a sheet or editing a cell) that serves as a trigger. We will use Google App Script’s UrlFetchApp class to make a request to post a message to Slack

var url = ' https://hooks.slack.com/services/ '; function sampleName(e) { return UrlFetchApp.fetch(url, params) }

Here’s Google’s full documentation for UrlFetchApp. The TLDR is, this is the class we want to call when we want to make data pull (GET) or data push (POST) requests to other applications over the internet.

We will be using its fetch method, which accepts two parameters — a url, which will receive the GET or POST request and all the parameters we want to pass which is represented by the variable params.

As you can see above, there are many things we can pass in our params variable. We just need the bare minimum- contentType, method and payload.

Here’s the full code so far:

var url = ' https://hooks.slack.com/services/ '; function sampleName(e) { var params = {

'contentType' : 'application/json',

'method' : 'POST',

'payload' : JSON.stringify(payload)

}; return UrlFetchApp.fetch(url, params) }

You’ll notice that for the payload, we are calling a variable named payload. This is where we will store our message. Referencing, Slack’s documentation, here’s what you can put inside the payload:

For this tutorial, we will keep it simple and just attach a text:

function sampleName(e){



var payload = {

'text' : "This is a sample text that will get posted on Slack"

} var params = {

'method' : 'post',

'contentType' : 'application/json',

'payload' : JSON.stringify(payload)

};



return UrlFetchApp.fetch(url, params) }

Step 3: Set up the trigger on Spreadsheets

We have one last thing to take care of and that is to choose an event that will trigger the posting of messages to Slack. There are readily available triggers in Spreadsheets. For this tutorial, we’ll use onEdit(e) which will run whenever someone changes a value in the Spreadsheet.

Once we’ve chosen an event to trigger the workflow, we can now do more such as identify who caused the event (e) using the user method or find out what the old value and new value of the cell is using the oldValue and value methods. These are all documented here.

We will make use of three methods mentioned above to give more context to our message on Slack. Here’s the full code by now:

function sampleName(e){ var newValue = e.value;

var oldValue = e.oldValue;

var user = e.user;

var payload = {

'text' : user + " just changed from " + oldValue + " to " + newValue + " :grimacing:"

}; var params = {

'method' : 'post',

'contentType' : 'application/json',

'payload' : JSON.stringify(payload)

};



return UrlFetchApp.fetch(url, params) }

You may have noticed, I added ‘:grimacing:’ in the text string. Yes, these will be interpreted as emojis when they get to Slack 😉

We are almost done! The last thing we need to do is assign the trigger to the function. Click Edit > Current project’s triggers to add a trigger:

Save your work and don’t forget to deploy under these settings:

And voila! Everytime there is new data in the Sheet, it gets posted on Slack:

fin