Last active
January 23, 2018 04:03
-
-
Save chalkpe/d7e7731b4a00d417741d5fd29334cbae to your computer and use it in GitHub Desktop.
is-orphan.gs
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 onOpen() { | |
SpreadsheetApp.getUi() | |
.createMenu('is-orphan') | |
.addItem('Run', 'run') | |
.addToUi() | |
} | |
function run () { | |
var myFiles = DriveApp.searchFiles('"me" in owners') | |
var orphanedFolder = DriveApp.getFoldersByName('Orphaned').next() | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0] | |
while (myFiles.hasNext()) { | |
try { | |
var file = myFiles.next() | |
var parents = getParents(file) | |
var info = getInformation(file).concat(parents.join('/')) | |
sheet.getRange('A2:H2').setValues([info]) | |
if (isOrphaned(parents)) { | |
orphanedFolder.addFile(file) | |
sheet.appendRow(info) | |
} | |
} catch (err) { | |
Logger.log(err) | |
} | |
} | |
} | |
function isOrphaned (parents) { | |
return parents.length === 0 || parents[0].getName() !== 'My Drive' | |
} | |
function getParents (file) { | |
var list = [] | |
while (file = getParent(file)) list.unshift(file) | |
return list | |
} | |
function getParent (file) { | |
try { return file.getParents().next() } | |
catch (err) { Logger.log(err) } | |
} | |
function getOwner (file) { | |
try { return file.getOwner().getName() + ' <' + file.getOwner().getEmail() + '>' } | |
catch (err) { Logger.log(err) } | |
} | |
function getInformation(file) { | |
return [ | |
new Date().toISOString(), | |
file.getId(), | |
file.getName(), | |
file.getMimeType(), | |
file.getSize(), | |
file.getLastUpdated().toISOString(), | |
getOwner(file) | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment