Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheckForNewFiles.gs
More file actions
Latest commit
101 lines (82 loc) · 3.08 KB
/
Copy pathcheckForNewFiles.gs
File metadata and controls
101 lines (82 loc) · 3.08 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
/***************************************************
This Google Script will send an slack notification to your Slack Channel
when a file in a Google Drive folder has been added, or modified.
***************************************************/
varROOT_FOLDER_ID='***************';
varSLACK_WEBHOOK='https://hooks.slack.com/services/************************';
varCHANNEL='Channel_name';
functioncheckForChangedFiles(){
varfolderList=listFolders(ROOT_FOLDER_ID)||[];
folderList=folderList.split(",");
for(vari=0;i<folderList.length;i++){
checkForChangedFilesInFolder(folderList[i]);
}
}
//Function to check which files have changed in the last 15 minutes
functioncheckForChangedFilesInFolder(folder_id){
varfolderID='"'+folder_id+'"';
varfolderSearch=folderID+" "+"in parents";
vartimezone=Session.getScriptTimeZone();
vartoday=newDate();
varfifteenMinutesAgo=newDate(today.getTime()-60*1000*15);
varstartTime=fifteenMinutesAgo.toISOString();
varsearch='(trashed = true or trashed = false) and '+folderSearch;
varfiles=DriveApp.searchFiles(search);
today=Utilities.formatDate(fifteenMinutesAgo,timezone,"yyyy-MM-dd HH:mm");
while(files.hasNext()){
varfile=files.next();
vardateCreated=Utilities.formatDate(file.getDateCreated(),timezone,"yyyy-MM-dd HH:mm")
if(dateCreated>today){
sendSlackNotification({
fileName: file.getName(),
fileURL: file.getUrl(),
fileOwner: file.getOwner().getName()
});
}
}
}
//Makes the payload and calls the function to send the message
functionsendSlackNotification(changedFile){
varpayload={
"icon_emoji": ":rolled_up_newspaper:",
"channel": "#"+CHANNEL,
"username": "Nueva Entrada",
"text": changedFile.fileOwner+" ha añadido un nuevo documento "+changedFile.fileName+" en "+changedFile.fileURL
};
sendMessageToSlack(payload);
}
//Send message to slack
functionsendMessageToSlack(payload){
varurl=SLACK_WEBHOOK;
varoptions={
'method': 'post',
'payload': JSON.stringify(payload)
};
varresponse=UrlFetchApp.fetch(url,options);
}
//Get all the subfolders from the root folder
functionlistFolders(id){
varparentFolder=DriveApp.getFolderById(id);
varchildFolders=parentFolder.getFolders();
varchilds=[];
while(childFolders.hasNext()){
varchild=childFolders.next();
childs.push(getSubFolders(child));
}
childs.push(id);
returnchilds.join(",");
}
//Get all the subfolders recursively
functiongetSubFolders(parent){
parent=parent.getId();
varchildFolder=DriveApp.getFolderById(parent).getFolders();
varchilds=[];
if(!childFolder.hasNext())returnparent;
while(childFolder.hasNext()){
varchild=childFolder.next();
varres=getSubFolders(child);
childs.push(res);
}
childs.push(parent);
returnchilds.join(",");
}