Skip to content

Instantly share code, notes, and snippets.

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 petrklus/2f34b2afe7b5067aec9dc00cf1a57d81 to your computer and use it in GitHub Desktop.
Save petrklus/2f34b2afe7b5067aec9dc00cf1a57d81 to your computer and use it in GitHub Desktop.
Google Spreadsheet script that lists all the shared documents inside a specified folder and all its subfolders recursively. Skips out the documents shared internally (with specified email addresses)
function start() {
const sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow(["Name", "Sharing Access", "Sharing Permission", "Get Editors", "Get Viewers", "Date", "Size", "URL", "Download", "Description", "Type"]);
const folder = DriveApp.getRootFolder();
recursiveListSharedFiles(folder, sheet);
}
function recursiveListSharedFiles(folder, sheet) {
listSharedFiles(folder, sheet);
const subfolders = folder.getFolders();
while (subfolders.hasNext()) {
recursiveListSharedFiles(subfolders.next(), sheet);
}
}
function listSharedFiles(folder, sheet) {
const files = folder.getFiles();
while (files.hasNext()) {
const file = files.next();
const externalEditorEmails = filterInternalEditors(file);
if (isOnlySharedInternally(externalEditorEmails, file)) {
continue;
}
appendRow(externalEditorEmails, file, sheet)
}
}
function filterInternalEditors(file) {
const internalEmails = [
"your.own@email.com",
"your.own@email2.com"
];
const editorEmails = file.getEditors().map(e => e.getEmail());
return editorEmails.filter(editorEmail => !internalEmails.includes(editorEmail));
}
function isOnlySharedInternally(externalEditorEmails, file) {
return externalEditorEmails.length === 0
&& file.getViewers().length === 0
&& file.getSharingAccess() === DriveApp.Access.PRIVATE;
}
function appendRow(editorEmails, file, sheet) {
const viewerEmails = file.getViewers().map(v => v.getEmail());
data = [
file.getName(),
file.getSharingAccess(),
file.getSharingPermission(),
editorEmails.toString(),
viewerEmails.toString(),
file.getDateCreated(),
file.getSize(),
file.getUrl(),
"https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(),
file.getDescription(),
file.getMimeType(),
];
sheet.appendRow(data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment