Skip to content

Instantly share code, notes, and snippets.

@danjargold
Created August 11, 2018 09:31
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save danjargold/c6542e68fe3a3b46eeb0172f914641bc to your computer and use it in GitHub Desktop.
Save danjargold/c6542e68fe3a3b46eeb0172f914641bc to your computer and use it in GitHub Desktop.
Google script to list (on a Google Sheet) all files shared in your google drive, including all viewers, editors, and sharing permissions. Credit goes to @woodwardtw (https://gist.github.com/woodwardtw/22a199ecca73ff15a0eb) as this is an improvement on his code which only assesses a single folder and one level of sub-folders down.
function listFolders(folder) {
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow(["Name", "Sharing Access", "Sharing Permission", "Get Editors", "Get Viewers", "Date", "Size", "URL", "Download", "Description", "Type"]); //writes the headers
//var folder = DriveApp.getFolderById("INSERT_YOUR_FILE_ID");//that long chunk of random numbers/letters in the URL when you navigate to the folder
//getLooseFiles(folder, sheet);
//getSubFolders(folder, sheet);
//instead of getting folder by ID rather get all folders and cycle through each. Note this will miss loose files in parent directory.
var folder = DriveApp.getFolders()
while (folder.hasNext()) {
var name = folder.next();
getLooseFiles(name, sheet);
getSubFolders(name, sheet);
};
}
function getLooseFiles(folder, sheet) {
var files = folder.getFiles();//initial loop on loose files w/in the folder
var cnt = 0;
var file;
while (files.hasNext()) {
var file = files.next();
var listEditors = file.getEditors(); //gets the editor email(s), doesn't show your own as it's assumed
var editors = [];
for (var cnt = 0; cnt < listEditors.length; cnt++) {
editors.push(listEditors[cnt].getEmail());
Logger.log(editors);
};
var listViewers = file.getViewers(); //gets the viewer email(s)
var viewers = [];
for (var cnt = 0; cnt < listViewers.length; cnt++) {
viewers.push(listViewers[cnt].getEmail());
Logger.log(viewers);
}
cnt++; //data chunk pushes all the file info to the ss
data = [
file.getName(),
file.getSharingAccess(),
file.getSharingPermission(),
editors.toString(),
viewers.toString(),
file.getDateCreated(),
file.getSize(),
file.getUrl(),
"https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(),
file.getDescription(),
file.getMimeType(),
];
if(data[1] != "PRIVATE") {
sheet.appendRow(data);
}
};
}
function getSubFolders(folder, sheet) {
var subfolders = folder.getFolders(); //same thing as above but for all the subfolders in the folder
while (subfolders.hasNext()) {
Logger.log(folder);
var name = subfolders.next();
getLooseFiles(name, sheet);
getSubFolders(name, sheet);
};
}
@mommalibrarian
Copy link

Code throws "Exception: The file or folder has invalid access settings." on the sharing permissions instructions. Discovering files with particular sharing permissions is the point of the code. How can the code anticipate, capture, and continue?

@moya-a
Copy link

moya-a commented Jun 26, 2021

Hey,

Thanks a lot for this, it helped me a lot.

I've made some improvments, so it's harder to timeout when there are lots of files involved.
(it took me about 3min for 500 files instead of timing out at 6mn).

I just cached all the files in a list and flushed them all at once into the spreadsheet instead of calling the API for each files.

you can check that out here : https://github.com/moya-a/G-Drive-SharedFiles-Checker

Cheers :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment