Skip to content

Instantly share code, notes, and snippets.

@JavierCane
Forked from woodwardtw/tellmeyoursecrets.js
Last active February 21, 2024 11:54
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save JavierCane/28f7307ceeaf6464431c1418b598a817 to your computer and use it in GitHub Desktop.
Save JavierCane/28f7307ceeaf6464431c1418b598a817 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.getFolderById("folder_id_copied_from_the_url_without_the_google_drive_domain");
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);
}
@ashebensaadon
Copy link

ashebensaadon commented May 8, 2022

Thanks. I'd add in line 43 another line to allow filtering by domain:

function filterInternalEditors(file) {
  const internalEmails = [
    "@gmail.com",
    "fullemail@gmail.com"
  ];

  const editorEmails = file.getEditors().map(e => e.getEmail());

  return editorEmails.filter(editorEmail =>
      !internalEmails.includes(editorEmail) &&
      !internalEmails.includes(editorEmail.slice(editorEmail.indexOf('@')))); //Line added
}

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