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);
};
}
@lhecht
Copy link

lhecht commented Feb 1, 2019

Hey thank you for that script, it is really close to what I was searching for. I cant believe, that google hasnt it build in.

I added a column with the name of the containing folder, which seemed usefull to me. Maybe you want to add this in your code? I am quite new to github, so I dont know where to add this as a pull request.
...,"Size", "Folder", "URL",...

folder.toString(),

I do have one question though. I run the script with the function listFolders as the chosen function. But it doesnt stop. What am I doing wrong? The sheet is produced and fine. Because I have only two folders yet, with around 20 files, there is no reason for it to not stop. Maybe it is because it searches in my pictures as well. Is there a fast way to exclude specific folders from the search path?

@lhecht
Copy link

lhecht commented Feb 1, 2019

Its me again :) I checked a little bit and it seems, that the function .getFolders() gives you ALL the children, not only the immediate children. This is maybe backed with this entry: https://stackoverflow.com/questions/41741520/how-do-i-search-sub-folders-and-sub-sub-folders-in-google-drive

So I think the whole subfolder part is obsolete. I commented it out and used for excluding the getParents() function. I dont know if you are interested in further developing your script. If so, I hoped this helps.

Cheers
Linos

@gmoeller
Copy link

The code throws an error when I try to run it... it says there is a problem with line 40.

The file or folder has invalid access settings. (line 40, file "Code")
Any suggestions?

@nerissa-c
Copy link

nerissa-c commented Dec 19, 2019

The code throws an error when I try to run it... it says there is a problem with line 40.

The file or folder has invalid access settings. (line 40, file "Code")
Any suggestions?

I commented out lines 42 and 43 and it seemed to work:
file.getSharingAccess()
file.getSharingPermission()

@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