Created
July 2, 2019 00:20
-
-
Save iaindooley/ccf5d0980fe2817a5275f96c2c3dd985 to your computer and use it in GitHub Desktop.
Bundle files into Drive
This file contains 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
function bundleFilesIntoDrive(notification) | |
{ | |
var card = new Notification(notification).addedLabel("Bundle").card(); | |
try | |
{ | |
var folder = DriveApp.getFoldersByName("Trellinator Downloads").next(); | |
} | |
catch(e) | |
{ | |
var folder = DriveApp.createFolder("Trellinator Downloads"); | |
} | |
var bundle = findOrCreateFolderByName(card.id()+"-attachments",folder); | |
card.attachments().each(function(att) | |
{ | |
try | |
{ | |
var file = Trellinator.downloadFileToGoogleDrive(att.link()); | |
bundle.addFile(file); | |
} | |
catch(e) | |
{ | |
Logger.log(e); | |
//Ignore errors, these files couldn't be saved | |
} | |
}); | |
card.attachLink({name: "Bundled Attachments",link: bundle.getUrl()}); | |
} | |
function findOrCreateFolderByName(filename,parent) | |
{ | |
var id = getFolderByName(filename,parent.getId()); | |
if(!id.id) | |
{ | |
var ret = parent.createFolder(filename); | |
} | |
else | |
{ | |
var ret = DriveApp.getFolderById(id.id); | |
} | |
return ret; | |
} | |
/* | |
* ****Get File By Name*** | |
* | |
*param 1: File Name | |
*param 2: Parent Folder of File (optional) | |
* | |
*returns: Dictionary of file "id" and "error" message {"id": ,"error": } | |
* -if there is no error, "id" returns file id and "error" returns false | |
* -if there is an error, "id" returns false and "error" returns type of error as a string for user to display or log. | |
*/ | |
function getFolderByName(fileName, fileInFolder) | |
{ | |
var filecount = 0; | |
var dupFileArray = []; | |
var folderID = ""; | |
var files = DriveApp.getFoldersByName(fileName); | |
while(files.hasNext()){ | |
var file = files.next(); | |
dupFileArray.push(file.getId()); | |
filecount++; | |
}; | |
if(filecount > 1){ | |
if(typeof fileInFolder === 'undefined'){ | |
folderID = {"id":false,"error":"More than one file with name: "+fileName+". \nTry adding the file's folder name as a reference in Argument 2 of this function."} | |
}else{ | |
//iterate through list of files with the same name | |
for(fl = 0; fl < dupFileArray.length; fl++){ | |
var activeFile = DriveApp.getFileById(dupFileArray[fl]); | |
var folders = activeFile.getParents(); | |
var folder = "" | |
var foldercount = 0; | |
//Get the folder name for each file | |
while(folders.hasNext()){ | |
folder = folders.next().getName(); | |
foldercount++; | |
}; | |
if(folder === fileInFolder && foldercount > 1){ | |
folderID = {"id":false,"error":"There is more than one parent folder: "+fileInFolder+" for file "+fileName} | |
}; | |
if(folder === fileInFolder){ | |
folderID = {"id":dupFileArray[fl],"error":false}; | |
}else{ | |
folderID = {"id":false,"error":"There are multiple files named: "+fileName+". \nBut none of them are in folder, "+fileInFolder} | |
}; | |
}; | |
}; | |
}else if(filecount === 0){ | |
folderID = {"id":false,"error":"No file in your drive exists with name: "+fileName}; | |
}else{ //IF there is only 1 file with fileName | |
folderID = {"id":dupFileArray[0],"error":false}; | |
}; | |
return folderID; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment