I need to detect the current spreadSheet user email address in a Google Sheet, then I will compare it to my limited users list to prevent my sheet from getting shared with everybody.
-
Related questions: [Google Apps Script - Get users email address](https://stackoverflow.com/q/43848448/1330560), [how to indentify user using google apps script?](https://stackoverflow.com/a/12173102/1330560), [Is there a script function to get user email when they edit a cell in a spreadsheet?](https://stackoverflow.com/a/69444255/1330560), [How can I get email of the last user who modified each cell](https://stackoverflow.com/q/60356262/1330560). – Tedinoz Apr 25 '23 at 13:19
-
Does this answer your question? [Google Apps Script - Get users email address](https://stackoverflow.com/questions/43848448/google-apps-script-get-users-email-address) – Tedinoz Apr 25 '23 at 13:20
2 Answers
There are a couple of things that can be helpful:
- To get the active user in the Google Sheet, you can use the following script and add a trigger to run that function when a user opens the sheet. Then you can compare the result with your users list.
function myFunction() {
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("A2").setValue(Session.getActiveUser());
}
Note: This option has some limitations, according to getActiveUser()
documentation , due to security restrictions this will work if users are within the same Google Workspace domain.
If your file is shared with anyone with the link, the best is to share it only with the users you want, then you can prevent those users from sharing your files:
- Open the file in Google Sheets.
- Click "Share".
- At the top, click "Settings".
- Uncheck "Editors can change permissions and share".
With Apps Script you can get the list of users that have access to your file, compare it with your limited users list and then remove the users that are not in your list. You can try the following sample:
function permissions() {
var driveFileID = "fileID"
var file = DriveApp.getFileById(driveFileID);
var users = Drive.Permissions.list(driveFileID).items;
var limitedUsers = ["admin@gmail.com", "example1@gmail.com", "example2@gmail.com"];
var listEmails = [];
for (var i = 0; i< users.length; i++){
let email = users[i].emailAddress;
listEmails.push(email)
}
var notFound = listEmails.filter(x=> !limitedUsers.includes(x) & x != null);
for (var a = 0; a < notFound.length; a++){
file.revokePermissions(notFound[a])
Logger.log("Removed user: "+ notFound[a])
}
}

- 1,946
- 2
- 4
- 11
Use the Share Spreadsheet feature to add specific users to your sheet.
You can then limit whether the users granted access are able to share with others by following these steps (see the “Limit how your files are shared” > “Prevent others from sharing your files” section)

- 352
- 1
- 10
-
The problem is the user still can make a copy of the spreadsheet and share it with others – Mo396 Apr 26 '23 at 21:41
-
There’s a permission to specifically not allow certain users to make a copy or share the sheet. – horanimal Apr 26 '23 at 22:39