Find out who has access to your Google Drive Files using Google Apps Script
Welcome!
In this blog we are going to find out who exactly has access to my Google Drive files, be it a Google Sheet, Google Doc, Form and more. To do this we are going to be using the DriveApp and Google Apps Script.
Recently while I was going through all my files, I found out that multiple people had access to my files but I wasn’t sure who exactly has access to my files and is my data safe.
To tackle this issue, we are going to be using the DriveApp and Google Apps Script.
So let’s get started.
Sample File
We are going to start out by having a sample Google Drive file that could be a Google Sheet, Doc or more. The Google Sheet that I am going to be using for this blog is just a very basic sheet that just contains names of people.
On creating the sheet, I have shared this sheet with two email addresses, in order to check if the code is able to fetch those users. If you want to work with the sheet, click on the link here.
If you want a video version of the blog, you can check out the video tutorial given below.
Step1: Write the Automation Script
While you are in the Google Sheet, let’s open up the Script Editor to write some Google Apps Script. To open the Script Editor, follow these steps:
- Click on Extensions and open the Script Editor.
2. This brings up the Script Editor as shown below.
We have reached the script editor lets code.
Once you have your script editor open, go ahead and fetch the id of your Google Drive File.
const fileId = "1EBSxejBO562cKMQFfRoK2aqW_7vvun-EBLxfQJYqHaU";
On fetching the ID of your file, go ahead and save that in a variable.
function getUsers() {
const file = DriveApp.getFileById(fileId);
const editors = file.getEditors();
const viewers = file.getViewers();
We are going to start by opening up a new function getUsers(), inside which we will use the getFileById() to fetch the file and pass the fileId variable as a parameter.
Once we have access to the file, we will use the getEditors() function and fetch all the editors of the particular file. We will be doing the same for viewers of a file.
const userEmails = [];
// Extract emails from editors
if (editors.length > 0) {
editors.forEach(editor => {
const email = editor.getEmail();
if (email) {
userEmails.push(email);
}
});
}
Now that we have access to the Editors and Viewers of the file, we will create an array to store all the email addresses. To fetch each editor of the file from the list, we are going to opening up an if loop that checks if there are any editors. If there are editors, we go ahead and fetch the email address of the editor and push it to the userEmails variable using the userEmails.push(email) function.
// Extract emails from viewers
if (viewers.length > 0) {
viewers.forEach(viewer => {
const email = viewer.getEmail();
if (email) {
userEmails.push(email);
}
});
}
We are going to be having the same code for fetching the viewers of the file and push it to the userEmails variable.
if (userEmails.length > 0) {
Logger.log("Users with access:");
userEmails.forEach(email => Logger.log(email));
} else {
Logger.log("No editors or viewers found for this file.");
}
}
Now that we have the list of all the users that have access to the file, we are going to go ahead and print these emails by opening up and if loop and then parsing the list using the forEach function.
Our code is complete and good to go.
Step3: Check the output
Its time to check the output and see if the code is working according to what we expected. To do that go ahead and save your code and run the getUsers() function.
On successful execution, you will see that all the users that have access to your file.
Conclusion
This is just the first and one of many articles that I am going to be writing around Google Drive and Google Apps Script. In this blog we looked at how we can get a list of users that have access to your Google Drive files using the DriveApp and Google Apps Script. You can get the code from the GitHub link given below.
Feel free to reach out if you have any issues/feedback at aryanirani123@gmail.com.