-
Notifications
You must be signed in to change notification settings - Fork 6
/
Code.gs
127 lines (107 loc) · 2.91 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
123
124
125
126
127
var SPREADSHEET_URL = 'https://docs.google.com/spreadsheets/d/.../edit';
var SLACK_URL = 'https://hooks.slack.com/services/...';
function doPost(e) {
var values = getValues(e);
if (SPREADSHEET_URL) {
handleSpreadsheet(values, SPREADSHEET_URL);
}
if (SLACK_URL) {
handleSlack(values, SLACK_URL);
}
return ContentService.createTextOutput('OK');
}
function getValues(e) {
var values = JSON.parse(e.postData.contents);
var answers = values["answers"];
if (typeof answers === "object") {
Object.keys(answers)
.forEach(function(answerLabel) {
var answer = answers[answerLabel];
values[answerLabel] = answer != null ? answer.toString() : answer;
});
delete values["answers"];
}
values['received_at'] = new Date();
return values;
}
function handleSpreadsheet(values, spreadsheetUrl) {
var ss = SpreadsheetApp.openByUrl(spreadsheetUrl);
var sheet = ss.getSheets()[0];
var headerMap = getExistingKeyColMap(sheet);
updateKeyColMap(sheet, headerMap, values);
writeValuesWithHeaderMap(sheet, headerMap, values);
}
function getExistingKeyColMap(sheet) {
var map = {};
sheet.getRange('1:1').getValues()[0].forEach(function(value, colIndex){
if (value.trim().length) {
map[value] = colIndex + 1;
}
});
return map;
}
function updateKeyColMap(sheet, headerMap, values) {
var maxColIndex = 0;
Object.keys(headerMap).forEach(function(key) {
maxColIndex = Math.max(maxColIndex, headerMap[key]);
});
Object.keys(values).forEach(function(valueKey) {
if (headerMap[valueKey] !== undefined) {
return;
}
// The values object has a key we haven't seen before.
maxColIndex += 1;
sheet.getRange(1, maxColIndex).setValue(valueKey);
headerMap[valueKey] = maxColIndex;
});
sheet.setFrozenRows(1);
var headerRow = sheet.getRange("1:1");
headerRow.setFontWeight("bold");
}
function writeValuesWithHeaderMap(sheet, headerMap, values) {
const rowIndex = sheet.getLastRow() + 1;
Object.keys(values).forEach(function(key) {
const colIndex = headerMap[key];
sheet.getRange(rowIndex, colIndex).setValue(values[key]);
});
}
function handleSlack(values, slackUrl) {
var fields = []
var text = Object.keys(values).forEach(function(key) {
var value = values[key];
fields.push({
"type": "mrkdwn",
"text": key
}, {
"type": "mrkdwn",
"text": "*" + value + "*"
});
});
var payload = JSON.stringify({
text: "New submission!",
blocks: [
{
"type": "section",
"fields": fields
}
]
});
var headers = {
"Accept":"application/json",
"Content-Type":"application/json"
};
var options = {
"method":"POST",
"headers": headers,
"payload": payload
};
var response = UrlFetchApp.fetch(slackUrl, options);
}
function test() {
doPost({
postData: {
type: "application/json",
contents: '{ "hello": "world" }',
}
})
}