-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
92 lines (77 loc) · 2.37 KB
/
script.js
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
function onOpen(e) {
SpreadsheetApp.getUi().createAddonMenu()
.addItem('Send to Slack', 'sendAllMessages')
.addToUi();
}
function sendAllMessages() {
const spreadsheetContent = getDataFromSpreadsheet();
const messages = createMessages(spreadsheetContent);
messages.forEach((message) => {
sendSlackMessage(message);
})
}
function validateColumnHeaders(range) {
const account = range.getCell(1, 1).getValue();
const assignment_demand = range.getCell(1, 2).getValue();
const start_date = range.getCell(1, 3).getValue();
const end_date = range.getCell(1, 4).getValue();
if (
!account.includes('Account') ||
!assignment_demand.includes('Assignment Demand') ||
!start_date.includes('Start Date') ||
!end_date.includes('End Date')
) {
throw new Error("There is a problem with the table headers");
}
}
function getDataFromSpreadsheet() {
const activeSpreadsheet = SpreadsheetApp.getActive();
const firstSheet = activeSpreadsheet.getSheets()[0];
const range = firstSheet.getDataRange();
validateColumnHeaders(range);
const values = range.getValues();
return values;
}
function createMessages(data) {
const schedulingDemands = data.slice(1).map(column => {
const account = column[0];
const assignment_demand = column[1];
const start_date = column[2].toLocaleDateString();
const end_date = column[3].toLocaleDateString();
const message = `*${assignment_demand}* \n${account}\n${start_date} to ${end_date}`
return message
});
const buildBlock = text => {
return {
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": text
}
}
]
}
}
const intro_message = buildBlock(":bell: *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.*");
let messages = [intro_message]
schedulingDemands.forEach((demand) => {
messages.push(buildBlock(demand));
})
return messages;
}
function sendSlackMessage(message) {
const webhookUrl = ScriptProperties.getProperty('WEBHOOK_URL');
const options = {
"method": "post",
"contentType": "application/json",
"muteHttpExceptions": true,
"payload": JSON.stringify(message)
};
try {
UrlFetchApp.fetch(webhookUrl, options);
} catch (e) {
Logger.log(e);
}
}