Created
March 5, 2019 22:41
-
-
Save wlizama/6fd88605dc92573458153fc5b3c9456f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GLOBALS | |
//Array of file extension which you would like to extract to Drive | |
var fileTypesToExtract = ['pdf', 'xml']; | |
//Name of the folder in google drive i which files will be put | |
var folderName = 'LabelMOD'; | |
//Name of the label which will be applied after processing the mail message | |
var labelName = 'LabelMOD'; | |
function GToDrive(){ | |
//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:'+formattedDate+ | |
//for(var i in fileTypesToExtract){ | |
// query += (query == '' ?('filename:'+fileTypesToExtract[i]) : (' AND filename:'+fileTypesToExtract[i])); | |
//} | |
query = 'in:inbox has:nouserlabels filename:pdf AND filename:xml OR (filename:pdf AND filename:zip)'; | |
var threads = GmailApp.search(query, 0 ,10); | |
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){ | |
var randomName = parseInt(Math.random()*10000000); | |
//get attachments | |
var attachments = mesgs[j].getAttachments(); | |
for(var k in attachments){ | |
var attachment = attachments[k]; | |
//var isImageType = checkIfImage_(attachment); | |
//if(!isImageType) continue; | |
var attachmentBlob = attachment.copyBlob(); | |
var file = DriveApp.createFile(attachmentBlob); | |
var fileType = file.getMimeType(); | |
var extension = ''; | |
if(fileType == 'application/pdf') | |
extension = 'pdf' | |
if(fileType == 'application/xml') | |
extension = 'xml' | |
if(fileType == 'application/zip') | |
extension = 'zip' | |
var newName = randomName + "." + extension; | |
file.setName(newName.replace(/\.\./g, '')) | |
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 today = new Date(); | |
var dateNDaysBack = new Date(today.valueOf() - n*24*60*60*1000); | |
return dateNDaysBack; | |
} | |
function getGmailLabel_(name){ | |
var label = GmailApp.getUserLabelByName(name); | |
if(label == null){ | |
label = GmailApp.createLabel(name); | |
} | |
return label; | |
} | |
//this function will check for filextension type. | |
// and return boolean | |
function checkIfImage_(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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment