-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_app_script_sample
159 lines (143 loc) · 4.65 KB
/
google_app_script_sample
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* The event handler triggered when editing the spreadsheet.
* @param {Event} e The onEdit event.
*/
//function onEdit(e) {
// var colToMonitor = 6;
// Set a comment on the edited cell to indicate when it was changed.
// if(e.range.getLastColumn() == colToMonitor) {
// Logger.log("I am here2")
// e.range.setNote('Last modified: '+e.range.getValue() );
// var parentFolder=DriveApp.createFolder(e.range.getValue());
//var newFolder=parentFolder.createFolder(e.getValue());
//var a = parentFolder.createFolder(e.range.getValue());
//}
//}
//function onOpen() {
// var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
//ui.createMenu('Law-E')
// .addItem('Create Case', 'createFolder')
// .addSeparator()
//.addToUi();
//}
//function createFolder() {
//SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
// .alert('You clicked the first menu item!');
//}
function createCase(e) {
var range = SpreadsheetApp.getActiveSheet().getActiveRange();
var parentFolderId = '1RqdBpwOqlzGP84p9XsEFhjgFGEufTFOm';
getOrCreateSubFolder(range.getValue(),parentFolderId);
}
function getOrCreateSubFolder(childFolderName, parentFolderId){
var parentFolder = DriveApp.getFolderById(parentFolderId);
var childFolders = parentFolder.getFoldersByName(childFolderName);
SpreadsheetApp.getUi().alert('checking folder.' +childFolders );
if (!childFolders.hasNext()) {
parentFolder.createFolder(childFolderName);
SpreadsheetApp.getUi().alert('case Created');
}
else {
SpreadsheetApp.getUi().alert('Its already there');
}
}
function isTrigger(funcName){
var r=false;
if(funcName){
var allTriggers=ScriptApp.getProjectTriggers();
for(var i=0;i<allTriggers.length;i++){
if(funcName==allTriggers[i].getHandlerFunction()){
r=true;
break;
}
}
}
return r;
}
function createOnEditTrigger() {
if(!isTrigger('createCase')){
ScriptApp.newTrigger('createCase').forSpreadsheet(SpreadsheetApp.getActive().getId()).onEdit().create();
}
}
/*
* Note:
* If you need any paid assistant, please write to [email protected]
* We provide Apps Script Development services at very reasonable price.
*/
// GLOBALS
//Array of file extension which you would like to extract to Drive
var fileTypesToExtract = ['jpg', 'tif', 'png', 'gif', 'bmp', 'svg'];
//Name of the folder in google drive i which files will be put
var folderName = 'GmailToDrive';
//Name of the label which will be applied after processing the mail message
var labelName = 'GmailToDrive';
function GmailToDrive(){
//build query to search emails
var query = '';
//filename:jpg OR filename:tif OR filename:gif OR fileName:png OR filename:bmp OR filename:svg'; //'after:'+getDateNDaysBack_(1)+
for(var i in fileTypesToExtract){
query += (query === '' ?('filename:'+fileTypesToExtract[i]) : (' OR filename:'+fileTypesToExtract[i]));
}
query = 'in:inbox has:nouserlabels ' + query;
var threads = GmailApp.search(query);
var label = getGmailLabel_(labelName);
var parentFolder;
if(threads.length > 0){
parentFolder = getFolder_(folderName);
}
var root = DriveApp.getRootFolder();
for(var i in threads){
var mesgs = threads[i].getMessages();
for(var j in mesgs){
//get attachments
var attachments = mesgs[j].getAttachments();
for(var k in attachments){
var attachment = attachments[k];
var isDefinedType = checkIfDefinedType_(attachment);
if(!isDefinedType) continue;
var attachmentBlob = attachment.copyBlob();
var file = DriveApp.createFile(attachmentBlob);
parentFolder.addFile(file);
root.removeFile(file);
}
}
threads[i].addLabel(label);
}
}
//This function will get the parent folder in Google drive
function getFolder_(folderName){
var folder;
var fi = DriveApp.getFoldersByName(folderName);
if(fi.hasNext()){
folder = fi.next();
}
else{
folder = DriveApp.createFolder(folderName);
}
return folder;
}
//getDate n days back
// n must be integer
function getDateNDaysBack_(n){
n = parseInt(n);
var date = new Date();
date.setDate(date.getDate() - n);
return Utilities.formatDate(date, Session.getScriptTimeZone(), 'yyyy/MM/dd');
}
function getGmailLabel_(name){
var label = GmailApp.getUserLabelByName(name);
if(!label){
label = GmailApp.createLabel(name);
}
return label;
}
//this function will check for filextension type.
// and return boolean
function checkIfDefinedType_(attachment){
var fileName = attachment.getName();
var temp = fileName.split('.');
var fileExtension = temp[temp.length-1].toLowerCase();
if(fileTypesToExtract.indexOf(fileExtension) !== -1) return true;
else return false;
}