forked from RajeshGogo/apps-script-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsheets2chat.gs
49 lines (45 loc) · 1.71 KB
/
sheets2chat.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
/**
* Posts a message to a Hangouts Chat room every time the spreadsheet is edited.
* This script must be attached to the spreadsheet (created in Google Sheets under
* "Tools > Script editor") and installed as a trigger:
* - Click "Edit > Current project's triggers" in the Apps Script UI.
* - Click "Add a new trigger".
* - Select the function "sendChatMessageOnEdit" and the event
* "From spreadsheet", "On edit".
* - Click "Save".
*
* @param {Object} e The onEdit event object.
*/
function sendChatMessageOnEdit(e) {
var range = SpreadsheetApp.getActiveRange();
var value = range.getValue();
var oldValue = e.oldValue;
var ss = range.getSheet().getParent();
// Construct the message to send, based on the old and new value of the cell.
var changeMessage;
if (oldValue && value) {
changeMessage = Utilities.formatString('changed from "%s" to "%s"',
oldValue, value);
} else if (value) {
changeMessage = Utilities.formatString('set to "%s"', value);
} else {
changeMessage = 'cleared';
}
var message = Utilities.formatString(
'The range %s was %s. <%s|Open spreadsheet>.',
range.getA1Notation(), changeMessage, ss.getUrl());
// Follow these steps to create an incomming webhook URL for your chat room:
// https://developers.google.com/hangouts/chat/how-tos/webhooks#define_an_incoming_webhook
var webhookUrl = 'ENTER INCOMMING WEBHOOK URL HERE';
// Use the spreadsheet's ID as a thread key, so that all messages go into the
// same thread.
var url = webhookUrl + '&threadKey=' + ss.getId();
// Send the message.
UrlFetchApp.fetch(url, {
method: 'post',
contentType: 'application/json',
payload: JSON.stringify({
text: message
})
});
}