-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.gs
122 lines (97 loc) · 3.52 KB
/
Code.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// 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://github.com/markfguerra/google-forms-to-slack
/////////////////////////
// Begin customization //
/////////////////////////
// Alter this to match the incoming webhook url provided by Slack
var slackIncomingWebhookUrl = 'https://hooks.slack.com/services/XXXXXX/XXXXXXXX/XXXXXXXXXXXXXX';
// Include # for public channels, omit it for private channels
var postChannel = "update_ps_XX";
var postIcon = ":mailbox_with_mail:";
var postUser = "Form Response";
var postColor = "#990000";
var messageFallback = "The attachment must be viewed as plain text.";
var messagePretext = "A <" + SpreadsheetApp.getActiveSpreadsheet().getUrl() + "|new issue> has been reported.";
///////////////////////
// End customization //
///////////////////////
// In the Script Editor, run initialize() at least once to make your code execute on form submit
function initialize() {
var triggers = ScriptApp.getProjectTriggers();
for (var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
ScriptApp.newTrigger("submitValuesToSlack")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit()
.create();
}
// Running the code in initialize() will cause this function to be triggered this on every Form Submit
function submitValuesToSlack(e) {
// Test code. uncomment to debug in Google Script editor
// if (typeof e === "undefined") {
// e = {namedValues: {"Question1": ["answer1"], "Question2" : ["answer2"]}};
// messagePretext = "Debugging our Sheets to Slack integration";
// }
var attachments = constructAttachments(e.values);
var payload = {
"channel": postChannel,
"username": postUser,
"icon_emoji": postIcon,
"link_names": 1,
"attachments": attachments
};
var options = {
'method': 'post',
'payload': JSON.stringify(payload)
};
var response = UrlFetchApp.fetch(slackIncomingWebhookUrl, options);
}
// Creates Slack message attachments which contain the data from the Google Form
// submission, which is passed in as a parameter
// https://api.slack.com/docs/message-attachments
var constructAttachments = function(values) {
var fields = makeFields(values);
var attachments = [{
"fallback" : messageFallback,
"pretext" : messagePretext,
"mrkdwn_in" : ["pretext"],
"color" : postColor,
"fields" : fields
}]
return attachments;
}
// Creates an array of Slack fields containing the questions and answers
var makeFields = function(values) {
var fields = [];
var columnNames = getColumnNames();
for (var i = 0; i < columnNames.length; i++) {
var colName = columnNames[i];
var val = values[i];
//fields.push(makeField(colName, val));
if(i == 6) {
fields.push(makeField("Submitted by", val));
}
}
return fields;
}
// Creates a Slack field for your message
// https://api.slack.com/docs/message-attachments#fields
var makeField = function(question, answer) {
var field = {
"title" : question,
"value" : answer,
"short" : false
};
return field;
}
// Extracts the column names from the first row of the spreadsheet
var getColumnNames = function() {
var sheet = SpreadsheetApp.getActiveSheet();
// Get the header row using A1 notation
var headerRow = sheet.getRange("1:1");
// Extract the values from it
var headerRowValues = headerRow.getValues()[0];
return headerRowValues;
}