Skip to content

Instantly share code, notes, and snippets.

@aculich
Forked from danjargold/whatFilesHaveIShared.gs
Last active September 21, 2021 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aculich/6b29cf1c4e252fa0623dc87b59ba6516 to your computer and use it in GitHub Desktop.
Save aculich/6b29cf1c4e252fa0623dc87b59ba6516 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);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment