-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimingCode.gs
More file actions
133 lines (109 loc) · 4.88 KB
/
timingCode.gs
File metadata and controls
133 lines (109 loc) · 4.88 KB
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
128
129
130
131
132
133
function dailycheck(){
var notice = "= Morning Brief =\n=== Pending ===\n"
checkAndSendEmail(notice)
}
function newlyApproved(){
var notice = "New Approval"
checkAndSendEmail(notice)
}
function timedCheckAndSendToSlack() {
// Get the active spreadsheet
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// Get the active sheet
var sheet = spreadsheet.getSheetByName(SHEET_NAME);
// Get the data range in column (APPROVAL column), starting from row 2 to skip the header
var range = sheet.getRange(2, findColumnByHeader(APPROVAL_HEADER), sheet.getLastRow() - 1, 1); // Column Approval, starting from row 2
// Get the values in the range
var values = range.getValues();
// Check if the number of rows in the range is less than 1, then pass
if (values.length < 1) {
return;
}
// Slack webhook URL
var slackWebhookUrl = SLACKWEBHOOK_URL;
// Loop through each row in the range
for (var i = 0; i < values.length; i++) {
// Check if the cell value in column contains 'APPROVED' and has not been posted yet 'POSTED_HEADER'
if (values[i][0] && values[i][0].toString().toUpperCase() === APPROVAL_TERM && sheet.getRange(i + 2, findColumnByHeader(POSTED_HEADER)).getValue() != 1) {
// Construct the message for the current item
var message = "The following extension has been approved:\n\n";
message += "Full Name: " + sheet.getRange(i + 2, findColumnByHeader(NAME_HEADER)).getValue() + "\n"; // Column
message += "Student ID: " + sheet.getRange(i + 2, findColumnByHeader(STUDENT_ID_HEADER)).getValue() + "\n"; // Column
message += "Assignment: " + sheet.getRange(i + 2, findColumnByHeader(ASSIGNMENT_HEADER)).getValue() + "\n"; // Column
message += "Notes: " + sheet.getRange(i + 2, findColumnByHeader(EXTENSION_NOTES)).getValue() + "\n"; // Column
message += "Approval: " + values[i][0] + "\n";
message += "Row: " + (i + 2); // Adding 2 because the data starts from row 2 + "\n";
// Construct payload for Slack
var payload = {
"text": message
};
// Send message to Slack
UrlFetchApp.fetch(slackWebhookUrl, {
"method": "post",
"contentType": "application/json",
"payload": JSON.stringify(payload)
});
// Set the posted slack cell to true
sheet.getRange(i + 2, findColumnByHeader(POSTED_HEADER)).setValue(1);
}
}
}
function checkAndSendEmail(notice) {
// Get the active spreadsheet
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// Get the active sheet
var sheet = spreadsheet.getSheetByName(SHEET_NAME);
// Get the data range in (APPROVAL column), starting from row 2 to skip the header
var range = sheet.getRange(2, findColumnByHeader(APPROVAL_HEADER), sheet.getLastRow() - 1, 1); // starting from row 2
// Get the values in the range
var values = range.getValues();
// Check if the number of rows in the range is less than 1, then pass
if (values.length < 1) {
return;
}
// Array to store approved items
var approvedItems = [];
// Loop through each row in the range
for (var i = 0; i < values.length; i++) {
// Check if the cell value contains 'APPROVED'
if (values[i][0] && values[i][0].toString().toUpperCase() === APPROVAL_TERM) {
// If 'APPROVED' is found, add details to approvedItems array
approvedItems.push({
row: i + 2, // Adding 2 because the data starts from row 2
assignment: sheet.getRange(i + 2, findColumnByHeader(ASSIGNMENT_HEADER)).getValue(), // Column
fullName: sheet.getRange(i + 2, findColumnByHeader(NAME_HEADER)).getValue(), // Column
studentID: sheet.getRange(i + 2, findColumnByHeader(STUDENT_ID_HEADER)).getValue(), // Column
notes: sheet.getRange(i + 2, findColumnByHeader(EXTENSION_NOTES)).getValue(), // Column
approval: APPROVAL_TERM // We know this entry is approved
});
}
}
// If there are approved items, send each item as a separate message to Slack
if (approvedItems.length > 0) {
// Slack webhook URL
var slackWebhookUrl = SLACKWEBHOOK_URL;
// Loop through each approved item
approvedItems.forEach(function(item) {
// Construct the message for the current item
var message = notice + "\n\n";
message += "Full Name: " + item.fullName + "\n";
message += "Student ID: " + item.studentID + "\n";
message += "Assignment: " + item.assignment + "\n";
message += "Notes: " + item.notes + "\n";
message += "Approval: " + item.approval + "\n";
message += "Row: " + item.row + "\n";
// Construct payload for Slack
var payload = {
"text": message
};
// Send message to Slack
UrlFetchApp.fetch(slackWebhookUrl, {
"method": "post",
"contentType": "application/json",
"payload": JSON.stringify(payload)
});
});
} else {
Logger.log("No "+ APPROVAL_TERM+" items found.");
}
}