Auditing “Shared with Me” Google Drive Files

The “Shared with Me” space in Google Drive can feel overwhelmingly chaotic and even anxiety inducing if you have the desire and need to organize everything in Google Drive. Here’s a simple Apps Script that will list the files that have been shared with you in a Google Sheet, along with the owner of that file and the date it was created. Simply paste the script below in the Script Editor of a Google Sheet, grant permissions to use it. Note: No data is written to the Google Sheet until the script has finished executing.

function onOpen() {
  SpreadsheetApp.getUi().createMenu("Shared with Me")
  .addItem('List files shared with me', 'sharedWithMe')
  .addToUi();
}

function sharedWithMe() {
  // Log the name of every file, date created and owner in the user's Drive that are considered "shared with me".
 const files = DriveApp.searchFiles('sharedWithMe');
 const newArray = [];
 while (files.hasNext()) {
   try{
   var file = files.next();
   Logger.log(file.getName());
   Logger.log(file.getDateCreated());
   Logger.log(file.getOwner().getName());
   }
   catch(e){
     Logger.log("Owner not found");
   }
   try{
   newArray.push([file.getName(),file.getDateCreated(),file.getOwner().getName()]);
   }catch(e){
     Logger.log("Owner not Found")
   }

 }
 
 Logger.log(newArray);
 Logger.log(newArray.length)
 SpreadsheetApp.getActiveSheet().clear();
 const header = SpreadsheetApp.getActiveSheet().getRange(1,1,1,3).setValues([["File Name","Date Created","Owner"]]);
 const data = SpreadsheetApp.getActiveSheet().getRange(2,1,newArray.length,3).setValues(newArray);
 
}