Skip to content

Instantly share code, notes, and snippets.

@so0k
Forked from jpsilvashy/README.md
Last active March 29, 2024 13:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save so0k/e71c1f1dbb9bad8dd5d7fd460c92679c to your computer and use it in GitHub Desktop.
Save so0k/e71c1f1dbb9bad8dd5d7fd460c92679c to your computer and use it in GitHub Desktop.
Post Google Sheets form entries to Slack

Post Google Sheets form entries to Slack

By using Google Form's script editor, you can call Slack webhooks when form submissions are made. You can use this script to do things like creating a live feedback form for taking questions from an audience or notifying your team when someone signs up for an event.

Setup

First, be sure you're collecting the email address in the form:

'img'

Create a form script

Next, open the script editor:

'img'

Paste the contents of javascript file in this gist into the script editor:

'img'

You may need to authorize access to Google Docs:

'img'

Create a Slack app

Go To https://api.slack.com/apps and create a new app,

Next, create an "incoming webhook" for your app, and swap in a url for the SLACK_WEBHOOK_POST_URL in our script in the script editor.

Then you'll need to edit the forms "triggers" so that our onSubmit function is called when a new form response is recorded:

'img'

'img'

Once you create the trigger, you're all done!

// This Google Sheets script will post to a slack channel when a user submits data to a Google Forms Spreadsheet
// View the README for installation instructions. Don't forget to add the required slack information below.
// Source: https://gist.github.com/jpsilvashy/f7e7ac5b8f34663079dff52274ab6b7b
// Alternative using Google Sheets:
// Source: https://github.com/markfguerra/google-forms-to-slack
// Put your Slack webhook here, make sure its connected to the correct channel
var SLACK_WEBHOOK_POST_URL = "https://hooks.slack.com/services/EXAMPLE";
function onSubmit(entry) {
// https://developers.google.com/apps-script/guides/triggers/events#form-submit_4
// Get item submitted
// event.response = https://developers.google.com/apps-script/reference/forms/form-response
var responses = entry.response.getItemResponses(); // Gets all item responses contained in a form response, in the same order that the items appear in the form
// Construct Slack message attachments
var fields = [
{
"title" : "Who",
"value" : entry.response.getRespondentEmail(),
"short" : false
},
{
"title" : "Resources",
"value" : responses[0].getResponse().join(), // For CheckboxItem questions, this returns a String[] array containing the responder's choices.
"short" : false
},
{
"title" : "Reason",
"value" : responses[1].getResponse(), // String for TextItem question
"short" : false
}
]
var attachments = [
{
"fallback" : "The attachment must be viewed as plain text.",
"pretext" : "Access requested",
"mrkdwn_in" : ["pretext"],
"color" : "#0000DD",
"fields" : fields
}
];
var payload = {
"attachments": attachments
};
// Build request
var options = {
method: "post",
payload: JSON.stringify(payload)
};
// Send to Slack
UrlFetchApp.fetch(SLACK_WEBHOOK_POST_URL, options);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment