File Permissions and Sharing Settings in Google Drive

F
Published in: Google Apps Script - Google Drive

This Google Script prints the sharing access and permissions of all files in your Google Drive. This helps you determine who has access to your Google Drive files. It uses the DriveApp service to retrieve all the files in your Google Drive and emails the report to the email address of the authorized Google user.

/*

  ======================================
  Who Can See Your Files in Google Drive
  ======================================

  Written by Amit Agarwal on 01/11/2014
  Tutorial :: http://labnol.org/?p=28237

*/


function ScanGoogleDrive() {

  var files = DriveApp.getFiles();
  var timezone = Session.getScriptTimeZone();
  var email = Session.getActiveUser().getEmail();

  var file, date, access, url, permission;
  var privacy, view, viewers, edit, editors;

  var rows = [["File Name", "Who has access?", "Date Created"]];

  while ( files.hasNext() ) {

    file = files.next();

    try {

      access     = file.getSharingAccess();
      permission = file.getSharingPermission();
      viewers    = file.getViewers();
      editors    = file.getEditors();

      view = [];
      edit = [];

      date =  Utilities.formatDate(file.getDateCreated(), timezone, "yyyy-MM-dd HH:mm")
      url = '<a href="' + file.getUrl() + '">' + file.getName() + '</a>';

      for (var v=0; v<viewers.length; v++) {
        view.push(viewers[v].getName() + " " + viewers[v].getEmail());
      }

      for (var ed=0; ed<editors.length; ed++) {
        edit.push(editors[ed].getName() + " " + editors[ed].getEmail());
      }

      switch(access) {
        case DriveApp.Access.PRIVATE:
          privacy = "Private";
          break;
        case DriveApp.Access.ANYONE:
          privacy = "Anyone";
          break;
        case DriveApp.Access.ANYONE_WITH_LINK:
          privacy = "Anyone with a link";
          break;
        case DriveApp.Access.DOMAIN:
          privacy = "Anyone inside domain";
          break;
        case DriveApp.Access.DOMAIN_WITH_LINK:
          privacy = "Anyone inside domain who has the link";
          break;
        default:
          privacy = "Unknown";
      }

      switch(permission) {
        case DriveApp.Permission.COMMENT:
          permission = "can comment";
          break;
        case DriveApp.Permission.VIEW:
          permission = "can view";
          break;
        case DriveApp.Permission.EDIT:
          permission = "can edit";
          break;
        default:
          permission = "";
      }

      view = view.join(", ");

      edit = edit.join(", ");

      privacy += (permission === "" ? "" : " " + permission)
               + (edit === "" ? "" : ", " + edit + " can edit")
               + (view === "" ? "" : ", " + view + " can view")

      rows.push([url, privacy, date]);

    } catch (e) {Logger.log(e.toString()); Logger.log(file.getName());};

  }

  var html = "<p>File Permissions Report for Google Drive</p>";

  html += "<table><tr><td><b>" + rows[0].join("</b></td><td><b>") + "</b></td></tr>";

  for (var i=1; i<rows.length; i++) {
    html += "<tr><td>" + rows[i].join("</td><td>") + "</td></tr>";
  }

  html += "</table><br>For help, refer to this <a href='http://www.labnol.org/internet/google-drive-access/28237/'>online tutorial</a> written by <a href='https://ctrlq.org/'>Amit Agarwal</a>.";

  MailApp.sendEmail(email, "Google Drive - File Permissions Report", "", {htmlBody: html});

}
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script - Google Drive

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube