This repository was archived by the owner on Aug 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSLACKmonitorChangesAndNotify.gs
More file actions
82 lines (66 loc) · 2.71 KB
/
Copy pathSLACKmonitorChangesAndNotify.gs
File metadata and controls
82 lines (66 loc) · 2.71 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
// Вставьте вашу ссылку Webhook, полученную на шаге 1
const SLACK_WEBHOOK_URL = 'https://hooks.slack.com/services/TXXXXXXXX/BXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX';
function monitorChangesAndNotifySlack() {
const sheetName = "test1";
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(sheetName);
if (!sheet) return;
const lastRow = sheet.getLastRow();
// Если данных нет (только заголовок или вообще пусто), выходим
if (lastRow < 1) return;
// Берем данные: A (URL), D (New Status), E (Old Status)
// Диапазон: A1:E_lastRow
const range = sheet.getRange(1, 1, lastRow, 5);
const data = range.getValues();
// Массив для обновления колонки E (память)
const updatesForColE = [];
// Флаг, чтобы не писать в лог/лист, если изменений не было
let changesFound = false;
for (let i = 0; i < data.length; i++) {
const siteUrl = data[i][0]; // Col A
const newStatus = data[i][3]; // Col D (индекс 3)
const oldStatus = data[i][4]; // Col E (индекс 4)
// Сравниваем
if (newStatus !== oldStatus) {
// Отправляем уведомление, только если новый статус не пустой
// (чтобы не спамить при очистке таблицы)
if (newStatus !== "") {
// Форматирование для Slack: *bold*, _italics_
const message = `⚠️ *Change Detected!*\n\n` +
`🌐 *Site:* ${siteUrl}\n` +
`🆕 *New Status:* ${newStatus}\n` +
`_Time: ${new Date().toLocaleString()}_`;
sendSlackMessage(message);
changesFound = true;
}
// Запоминаем новый статус
updatesForColE.push([newStatus]);
} else {
// Оставляем старый
updatesForColE.push([oldStatus]);
}
}
// Обновляем колонку E массово
if (changesFound) {
sheet.getRange(1, 5, updatesForColE.length, 1).setValues(updatesForColE);
Logger.log("Slack notifications sent and history updated.");
} else {
Logger.log("No changes detected.");
}
}
function sendSlackMessage(text) {
const payload = {
"text": text
};
const options = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(payload),
'muteHttpExceptions': true
};
try {
UrlFetchApp.fetch(SLACK_WEBHOOK_URL, options);
} catch (e) {
Logger.log("Slack Error: " + e.message);
}
}