Instantly share code, notes, and snippets.
-
Save arran4/c10d3782d103e016c85f7eeed347ee68 to your computer and use it in GitHub Desktop.
A script to list all your shared files in google drive and with whom it's shared (forked.)
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
// Run this function to start! You will get an email after a while. ATM it runs at a rate of 20 files every 5 minutes, then it should email you when done. This can be a long time later depending on how many files you have. | |
function createTimeDrivenTriggers() { | |
var scriptProperties = PropertiesService.getScriptProperties(); | |
scriptProperties.setProperty("EMAIL", Session.getActiveUser().getEmail()); | |
ScriptApp.newTrigger('listFilesInDrive') //run "listFilesInDrive()" every 5 minutes | |
.timeBased() | |
.everyMinutes(5) //Runs every 5 minutes | |
.create(); | |
}; | |
function reset() { | |
var scriptProperties = PropertiesService.getScriptProperties(); | |
scriptProperties.deleteProperty("SHEET_ID"); | |
scriptProperties.deleteProperty("LAST_EXECUTION"); | |
scriptProperties.deleteProperty("IMPORT_ALL_FILES_CONTINUATION_TOKEN"); | |
scriptProperties.deleteProperty("EMAIL"); | |
} | |
function listFilesInDrive() { | |
var scriptProperties = PropertiesService.getScriptProperties(); | |
var MAX_FILES = 20; //use a safe value, don't be greedy! | |
var sheetId = scriptProperties.getProperty('SHEET_ID'); | |
var ss; | |
if (sheetId == null) { | |
var date = new Date(); | |
ss = SpreadsheetApp.create("Shared audit " + date.toISOString()); | |
scriptProperties.setProperty("SHEET_ID", ss.getId()); | |
} else { | |
ss = SpreadsheetApp.openById(sheetId); | |
} | |
var sheet = ss.getActiveSheet(); | |
var files = DriveApp.getFiles(); | |
var count = 0; | |
var lastExecution = scriptProperties.getProperty('LAST_EXECUTION'); | |
if( lastExecution === null ){ | |
lastExecution = ''; | |
} | |
var lastCount = scriptProperties.getProperty('COUNT'); | |
if (lastCount != null) { | |
count = lastCount; | |
} | |
var continuationToken = scriptProperties.getProperty('IMPORT_ALL_FILES_CONTINUATION_TOKEN'); | |
var iterator = continuationToken == null ? | |
DriveApp.getFiles() : DriveApp.continueFileIterator(continuationToken); | |
if (continuationToken == null) { | |
sheet.clear(); | |
sheet.appendRow(["Shared URL", "Name","Owner","Editors with","New editors with","View with","New view with"]); | |
sheet.setFrozenRows(1); | |
} | |
try { //while script has processed less than MAX_FILES | |
for( var i = 0; i < MAX_FILES && iterator.hasNext(); ++i ) { | |
var file = iterator.next(); | |
//var dateCreated = formatDate(file.getDateCreated()); | |
count++; | |
//if(dateCreated > lastExecution) { | |
Logger.log("Up to file " + file.getName() + " " + count); | |
var fileData = processFile(file); | |
if (fileData == null) { | |
continue; | |
} | |
sheet.appendRow(fileData); | |
//} | |
} | |
} catch(err) { | |
Logger.log(err); | |
MailApp.sendEmail({ | |
to: scriptProperties.getProperty('EMAIL'), | |
subject: 'Error', | |
htmlBody: "<pre>" + Logger.getLog() + "</pre>" | |
}); | |
} | |
//if there is still more files after MAX_FILES were processed | |
if( iterator.hasNext() ) { | |
scriptProperties.setProperty('IMPORT_ALL_FILES_CONTINUATION_TOKEN', iterator.getContinuationToken()); | |
scriptProperties.setProperty('COUNT', count); | |
var currentUser = Session.getActiveUser().getEmail(); | |
//MailApp.sendEmail({ | |
// to: currentUser, | |
// subject: 'Running loop', | |
// htmlBody: "Count: " + count + "<br />\n<pre>" + Logger.getLog() + "</pre>" | |
// }); | |
} else { // Finished processing files so delete continuation token | |
// Deletes all triggers in the current project. | |
Logger.log("Done."); | |
var triggers = ScriptApp.getProjectTriggers(); | |
for (var j = 0; j < triggers.length; j++) { | |
ScriptApp.deleteTrigger(triggers[j]); | |
} | |
var currentUser = scriptProperties.getProperty('EMAIL'); | |
MailApp.sendEmail({ | |
to: currentUser, | |
subject: 'Your shared Google Drive files', | |
body: "Done. url:" + sheet.getUrl() | |
}); | |
scriptProperties.deleteProperty("SHEET_ID"); | |
scriptProperties.setProperty('LAST_EXECUTION', formatDate(new Date())); | |
scriptProperties.setProperty('COUNT', 0); | |
sheet.appendRow(["All files found", formatDate(new Date())]); | |
//visual confirmation all files found | |
var lastRow = sheet.getLastRow(); //turn last row YELLOW and BOLD | |
var lastRowCells = sheet.getRange(lastRow,1,1,2); | |
lastRowCells.setFontWeight("bold"); | |
lastRowCells.setBackground("#ffffb8"); | |
var HeaderRowCells = sheet.getRange("A1:H1"); //turn Header row ORANGE and BOLD | |
HeaderRowCells.setBackground("#f0b75b"); | |
HeaderRowCells.setFontWeight("bold"); | |
} | |
}; | |
function formatDate(date) { | |
return Utilities.formatDate(date, "GMT", "yyyy-MM-dd HH:mm:ss"); | |
}; | |
function processFile(file) { | |
var fileOwner; | |
try { | |
fileOwner = file.getOwner().getEmail(); | |
} catch(e) { | |
Logger.log('Error retrieving file owner email address for file ' + file.getName() + ' with the error: ' + e.message); | |
} | |
var url = file.getUrl(); | |
var editWith = ""; | |
var viewWith = ""; | |
var shares = 0; | |
//Logger.log(file.getEditors()); | |
var editors = file.getEditors(); | |
for (var e in editors) { | |
var each = editors[e]; | |
//Logger.log(each); | |
editWith = editWith + each.getEmail() + "; "; | |
shares++; | |
} | |
var viewers = file.getViewers(); | |
for (var e in viewers) { | |
var each = viewers[e]; | |
//Logger.log(each); | |
viewWith = viewWith + each.getEmail() + "; "; | |
shares++; | |
} | |
if (shares == 0) { | |
Logger.log("Skipping no shares"); | |
return null; | |
} | |
var name = file.getName(); | |
var fileData = [url, name,fileOwner,editWith,"", viewWith, ""] | |
return fileData; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment