I Recently had to share some documents from my Google Drive with a team of developers outside the office. Most of these documents were API docs. After a couple of months and when those developers didn’t need the API docs anymore, it was almost impossible for me to find what i shared with them, it was like looking for a needle in a haystack. I bet i am not the only one that had this problem before!!!
So how would i do that manually. I would go to each file and click on share button. see the list of the permission and remove the users i don’t want to. It means that i would need to go through 200 files!!! I am too lazy for that!!
Since i like Ruby programming i thought about using the Google Drive API to accomplish this task. I looked at this gem and i was immediately discouraged by it’s complexity. It has to be a better solution!!!
After looking at Google Script overview page i realized that this is going to be fairly easy to solve an amazing complex problem. Google Script use a language based on Javascript and this API doc.
This code is largely inspired from Amit Agarwal code.
functionStart(){varfiles=DriveApp.getFiles();vartimezone=Session.getScriptTimeZone();varemail=Session.getActiveUser().getEmail();varMAXFILES=300;count=1;varfile,date,access,url,permission;varprivacy,view,viewers,edit,editors;varrows=[["File Name","Who has access?","Date Created"]];for(vari=0;i<MAXFILES&&files.hasNext();i++){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=count+++'. <a href="'+file.getUrl()+'">'+file.getName()+'</a>';for(varv=0;v<viewers.length;v++){view.push(viewers[v].getName()+" "+viewers[v].getEmail());}for(vared=0;ed<editors.length;ed++){edit.push(editors[ed].getName()+" "+editors[ed].getEmail());}switch(access){caseDriveApp.Access.PRIVATE:privacy="Private";break;caseDriveApp.Access.ANYONE:privacy="Anyone";break;caseDriveApp.Access.ANYONE_WITH_LINK:privacy="Anyone with a link";break;caseDriveApp.Access.DOMAIN:privacy="Anyone inside domain";break;caseDriveApp.Access.DOMAIN_WITH_LINK:privacy="Anyone inside domain who has the link";break;default:privacy="Unknown";}switch(permission){caseDriveApp.Permission.COMMENT:permission="can comment";break;caseDriveApp.Permission.VIEW:permission="can view";break;caseDriveApp.Permission.EDIT:permission="can edit";break;default:permission="";}view=view.join(", ");edit=edit.join(", ");privacy+=(permission===""?"":" "+permission)+(edit===""?"":"<br><small>"+edit+" can edit</small>")+(view===""?"":"<br><small>"+view+" can view</small>")rows.push([url,privacy,date]);}catch(e){Logger.log(e.toString());Logger.log(file.getName());};}varhtml="<p style='text-align:center'><strong><a style='font-size:160%;text-decoration:none;color:#49B3F5;' File Permissions Report for Google Drive</a></strong></p>";html+="<table border='1' cellpadding='5' cellspacing='0'><tr><td><b>"+rows[0].join("</b></td><td><b>")+"</b></td></tr>";for(vari=1;i<rows.length;i++){html+="<tr><td>"+rows[i].join("</td><td>")+"</td></tr>";}html+="</table>";GmailApp.sendEmail(email,"Google Drive - File Permissions Report","",{htmlBody:html});}