Google Apps Script project that gets data from a Google Sheets spreadsheet, transforms it to create a Slackbot payload and sends one alert per spreadsheet row. It has also been published as a private Sheets Add-on in a Google Cloud Project and is available to install for Made Tech users through the Google Workspace Marketplace.
When a spreadsheet is opened, this script adds an item to the add-on menu at: Extensions > Scheduling Demand Slackbot > Send to Slack.
Clicking on Send to Slack triggers the following workflow:
- Read the data of the first sheet of the spreadsheet.
- Check that the column headers and column order are as expected (see below). Exit and throw an error if not.
- Format the data for each row into a message
string
. - Create a
JSON
payload for each message using Slack message blocks syntax. - For each message call the Slackbot Webhook URL with the relevant payload. This will send a message for each role in the designated Slack channel.
The script validates the first 4 column headers by cheking that they match the below:
Account | Assignment Demand | Start Date | Element End Date |
---|
The script sends the following intro message to Slack:
🔔 Hello, here are the latest billable roles we are looking to fill. Please reply in the relevant thread if you're interested in a particular role.
And follows with one message per role which is structured like this:
Assignment Demand
Account
Start Date to Element End Date
Example:
Software Engineer_SFIA 1 Academy - DVLA Investment & Account Oversight
DVLA
27/09/2022 to 30/11/2022
For local development you will need to install:
- Node Js + NPM - Node 4.7.4 or later,
- clasp - clasp is an open source tool that allows you to develop and manage Apps Script projects from your terminal rather than the Apps Script editor.
You will also need to enable the Google Apps Script API because that’s what clasp uses in the background. In order to do this please:
- Navigate to the Apps Script Settings page.
- Click on "Google Apps Script API".
- Toggle the switch to "on".
-
Get the ScriptID from the mother Apps Script project (which needs to have been shared with you beforehand):
- Navigate to My Projects or Shared with me.
- Find the project and open it.
- Open the project settings (the cog icon on the left hand side).
- Under IDs, find the ScriptID and copy it for later.
-
Clone this repository with
git clone https://github.com/madetech/scheduling-demand-slackbot.git
. -
Install clasp with
sudo npm install @google/clasp -g
-
Log into clasp with
clasp login
-
A window will open, and ask you to choose a Google account (make sure you choose the one associated with the spreadsheet)
-
Click “Allow” on the next screen
-
Run
clasp clone <script_id>
to connect your local repository with the Google Scripts App project (this is where you paste in the script ID from the Apps Script project) -
This should add three files to the current directory:
- A
.clasp.json
file storing the script ID. - An
appsscript.json
project manifest file containing project metadata. - A
script.js
file containing on empty function.
- A
-
Create a
.claspignore
file and add the following:README.md .gitignore .clasp.json
In order to make a change to the script please follow the steps below:
- Make the code change in your local environment.
- Push the changes to AppScript with
clasp push
(this will allow you to run the code and see the change in action). - Make a commit & push to github.
In oder to deploy the change please follow the steps below:
- Create a new version of the script in the App Scripts project:
- Open the App Scripts project.
- Click the arrow on the 'Deploy' button.
- Select 'Manage deployments'.
- Click the pencil icon to go into the edit mode.
- Select the 'New version' in the 'Version' dropdown.
- Click 'deploy'.
- Click 'done'.
- Update the version of the script in the Google Cloud project:
- Go to the Google Cloud project.
- Put 'workspace marketplace SDK' in the search box at the top & navigate to it.
- Click 'Manage'.
- Go to 'APP CONFIGURATION'.
- Put the new version of the script in the 'Sheets Add-on script version' section.
- Click 'save'.
Make sure you've been added as a collaborator to the "Scheduling Demand Slackbot" app. Then:
-
Set up a new Slack Webhook:
- In a browser go to your Slack apps.
- Click on the app called "Scheduling Demand Slackbot".
- Click on "Incoming Webhooks" (left hand menu).
- If you want to delete the Webhook connecting the old Slack channel to the add-on, click on the bin icon of that Webhook.
- Click on "Add New Webhook to Workspace".
- Choose the new channel from the dropdown and choose "Allow".
- Copy the new Webhook URL.
-
Open the mother Apps Script project.
-
Navitgate to the project settings and scroll down to "Script Properties".
-
There should be a property called "WEBHOOK_URL". Click "Edit script properties" and paste the new URL into the value field.
-
Test that everything works by Running the script.
-
Once tested, you can publish your changes as an add-on (follow above deploy steps).
The Add-On only needs to be installed once. In order to do it, please follow the steps below:
- Open a spreadsheet in Google Sheets.
- Open 'Extensions' > 'Add-ons' > 'Get add-ons' in the top menu.
- Click 'Internal apps' button.
- Click 'Scheduling Demand Slackbot'.
- Click 'Install' button.
- If prompted, click 'Continue' and give the necessary permissions.
This will install the script in your Google Sheets. You can check if it's been installed correctly by clicking on the 'Extensions' in the top menu. Now you should see 'Scheduling Demand Slackbot' menu item. If it doesn't show, please refresh the document and wait a few seconds.
Open 'Extensions' > 'Scheduling Demand Slackbot' from the top menu and click 'Send to Slack'.
If you get an error message with the following text "Error: There is a problem with the table headers" ensure that the columns of the spreadsheet are in the correct order and have the right names (see above).