Before

After

In this tutorial, I wanted to briefly show how to send any data from Google Sheets into a Slack channel. We'll be using the Slack incoming webhook documentation, which you can find here.

As an example, I've inserted very little data into the sheet, but you can, of course, do the same with much more data.

To get started, make a copy of the above Google Sheet and open your script editor by going to "Tools" -> "Script editor".

Step 1 - Create a webhook URL

To authenticate your script, you need to create a unique webhook URL so that you can access your Slack environment. This is quite straightforward and can be done here: https://my.slack.com/services/new/incoming-webhook

Step 2 - Define input values

For the purpose of this tutorial, I've added random data n range "A1:A4" and will use that. In your case, you probably want to define a different data range. Below is the code that you can use to do so.

//Define in what sheet to import values from
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var importDate = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy");
var data2 = sheet.getRange("A2").getValue();
var data3 = sheet.getRange("A3").getValue();
var data4 = sheet.getRange("A4").getValue();

Step 3 - Customise payload

When you created your webhook URL, you most likely already set certain characteristics such as the channel name and the name of the bot. You will be able to overwrite these inside the script by editing the payload variables below.

var payload = {
    //CHANNEL
    //ncoming webhooks have a default channel, but it can be overridden in your JSON payload. 
    //A public channel can be specified with "channel": "#other-channel", and a Direct Message with "channel": "@username".
     "channel" : "#random",  
    
    //USERNAME
    //You can customize the name and icon of your Incoming Webhook
    //Customise the displayed name by for instance sending "username": "new-bot-name"  
     "username" : "Stats Bot",
    
    //ICON
    //You can also override the bot icon either with "icon_url": "https://slack.com/img/icons/app-57.png" or "icon_emoji": ":ghost:"
    //"icon_url" : "http://image"
    "icon_emoji": ":rocket:",
    
    //TEXT
    //To create a link in your text, enclose the URL in <> angle brackets. 
    //For example: payload="text": "" will post a clickable link to https://slack.com. 
    //To display hyperlinked text instead of the actual URL, use the pipe character, as shown in this example:
    "text" : importDate + "\nMetric 1: " + data2 + "\nMetric 2: " + data3 + "\nMetric 3: " + data4 
    
    //To format your text --> https://api.slack.com/reference/surfaces/formatting#visual-styles
  };

Step 4 - Send data to Slack

Once you've customised the payload, you're ready to send the data to Slack. You can either test this out by hitting "play" and check if everything went fine. You might want to format the message a bit better using white lines (e.g. "/n") or other formatting. 
 
If you're satisfied with the final message, then you can set up a recurring trigger by clicking on the stopwatch icon. That way the script will run automatically each minute, day, hour or whichever set-up you decide. 

//Tell the API to post
var options =  {
  "method" : "post",
  "contentType" : "application/json",
  "payload" : JSON.stringify(payload)
};
  
//Make the API post request
return UrlFetchApp.fetch(url, options)




Let me know if this was useful & let's connect on     or