-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.gs
More file actions
85 lines (69 loc) · 2.45 KB
/
Code.gs
File metadata and controls
85 lines (69 loc) · 2.45 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
/**
A simple webhook function for change events.
Assumes a header row in the first row of the sheet.
When a change occurs, data will be taken from the active row.
Headers are whitespace-stripped and then used as property names
to create an object hash of the data.
The user may implement the doesQualify(entity) function to implement
conditional webhook triggering.
That data is used in an application/json POST to the endpoint provided.
The form of the data is:
{
metadata: {
spreadsheetId: (id of spreadsheet),
sheetName: (name of sheet),
row: (1-based row number where data was modified).
},
entity: (the entity object produced)
}
*/
function onEdit(event){
var ENDPOINT = "https://runflow.built.io/run/2rhEDpFYwU"; //PropertiesService.getDocumentProperties().getProperty("Endpoint");
if(ENDPOINT){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var row = sheet.getActiveRange().getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
var data = sheet.getRange(row, 1, 1, sheet.getLastColumn()).getValues()[0];
if(row !== 1 && data){
var entity = {};
headers.forEach(function(header, i){
var propertyName = header.replace(/[\W\s]/g, "");
if(propertyName){
var propertyValue = data[i];
if(propertyValue){
entity[propertyName] = propertyValue;
} else {
entity[propertyName] = null;
}
}
});
if(doesQualify(entity)){
var thePayload = {
metadata: {
spreadsheetId: ss.getId(),
sheetId: sheet.getSheetId(),
sheetName: sheet.getName(),
rowNumber: row
},
entity: entity
};
Logger.log( thePayload );
var params = {
method: "POST",
contentType: "application/json",
payload: JSON.stringify(thePayload)
};
var response = UrlFetchApp.fetch(ENDPOINT, params);
Logger.log( response );
}
}
}
}
/**
This function returns true by default. Override it to provide qualification logic
for whether a webhook should be sent or not.
*/
function doesQualify(entity){
return entity.FirstName && entity.LastName && entity.Email && entity.CompanyName;
}