Google (App or Apps) Script Read File From Drive
This script volition expect for attachments in Gmail Bulletin and Extract it to a Google Drive binder. Script can be run manually or through a time driven trigger. The script tin can exist personalized by following the instructions
given in this article.
- Brand a copy of this script project
- In the new copy of the script, go to Resources > Current Project'southward triggers
Click on the link displayed to setup a fourth dimension driven trigger and save the trigger. Frequency depends on how frequently y'all want to look for new letters in Gmail. Here is a screenshot of the trigger settings to run every minute.Click on notifications and choose your notification preferences. This will notify you if at that place occurs execution failure. Here is an screenshot of the notification preferences
- If you are running the script first time, it will enquire you to qualify the script for "Gmail" and "Google Drive" admission. Authorize it. Now the script installation is complete.
This procedure can be followed with any Gmail account. Once the installation is complete, script will run as per the specified interval, look for messages in Gmail which are not labelled and have attachments as file types specified. It will extract the attachment to a Google Bulldoze folder and characterization the Gmail Message 'GmailToDrive'.
Y'all may change line 3, five and seven as per your preferences.
var fileTypesToExtract = [ 'jpg' , 'tif' , 'png' , 'gif' , 'bmp' , 'svg' ];
This is an array for file types to look for. You may add or remove file types from this array.
var folderName = 'GmailToDrive' ;
This is the proper noun of the folder to which files will be extracted to in Google Bulldoze. If the script does not finds a folder with this name, it volition create the folder in bulldoze.
var labelName = ' GmailToDrive ' ; This label name is Gmail Label proper name. This is used by the script to identify which bulletin has been candy and which has not not. One time a bulletin is processed, script will put a label on those messages with below proper noun. You may change the label name too.
/*
* Note:
* If you need any paid assistant, please write to support.waqar@gmail.com
* Nosotros provide Apps Script Development services at very reasonable cost.
*/
// GLOBALS
//Array of file extension which you lot would like to excerpt to Bulldoze
var fileTypesToExtract = ['jpg', 'tif', 'png', 'gif', 'bmp', 'svg'];
//Name of the folder in google bulldoze i which files will be put
var folderName = 'GmailToDrive';
//Name of the characterization which will be applied afterward processing the mail service 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'; //'subsequently:'+formattedDate+
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 one thousand in attachments){
var attachment = attachments[1000];
var isDefinedType = checkIfDefinedType_(zipper);
if(!isDefinedType) continue;
var attachmentBlob = attachment.copyBlob();
var file = DriveApp.createFile(attachmentBlob);
parentFolder.addFile(file);
root.removeFile(file);
}
}
threads[i].addLabel(label);
}
}
//This function volition go the parent folder in Google drive
part getFolder_(folderName){
var folder;
var fi = DriveApp.getFoldersByName(folderName);
if(fi.hasNext()){
binder = fi.next();
}
else{
folder = DriveApp.createFolder(folderName);
}
return folder;
}
//getDate northward days back
// n must be integer
function getDateNDaysBack_(n){
due north = parseInt(northward);
var engagement = new Appointment();
date.setDate(engagement.getDate() - n);
render Utilities.formatDate(date, Session.getScriptTimeZone(), 'yyyy/MM/dd');
}
part getGmailLabel_(proper noun){
var characterization = GmailApp.getUserLabelByName(name);
if(!label){
label = GmailApp.createLabel(proper noun);
}
return characterization;
}
//this part will check for filextension type.
// and return boolean
function checkIfDefinedType_(attachment){
var fileName = attachment.getName();
var temp = fileName.split up('.');
var fileExtension = temp[temp.length-ane].toLowerCase();
if(fileTypesToExtract.indexOf(fileExtension) !== -one) return true;
else return false;
}
Source: http://www.googleappsscript.org/home/fetch-gmail-attachment-to-google-drive-using-google-apps-script
0 Response to "Google (App or Apps) Script Read File From Drive"
Post a Comment