The following Google Apps Script is designed to change the Google Drive file permissions of a specific user from Editor to Viewer. The function actually came about when needing to end a process where a user had been editing a Google Sheet on a Shared drive that they should only then have Viewer access to. To complicate matters the Apps Script code is running as said user when it needs to reduce their own permissions.
Firstly you will need to enable the Drive API in the Script Editor by going to 'Services' > 'Drive API' > 'Add'. Next we will look to get the permission ID for the email address of the user we want to change access for:
var permissionId = Drive.Permissions.getIdForEmail('email address here').id;
Then we need to set the new role that our user will have, an option to include Shared drives and prevent any automated emails, in a permission resource:
var resource = {
role: 'reader',
};
supportsAllDrives: true,
sendNotificationEmails: false
Finally we call the Drive API to make the change:
Drive.Permissions.update(resource, 'file ID here', permissionId);
function changeAccess() { | |
// email address of the account to change access for | |
var emailAddress = 'EMAIL ADDRESS HERE'; | |
// ID of Google file to change access to | |
var fileId = 'FILE ID HERE'; | |
// get Permission ID for given email address | |
var permissionId = Drive.Permissions.getIdForEmail(emailAddress).id; | |
// create a resource for the permission to change to | |
var resource = { | |
// enter new role here | |
role: 'reader' | |
}; | |
// update permissions using the Drive API | |
Drive.Permissions.update(resource, fileId, permissionId); | |
} |
Thanks so much! it helps me a lot.
ReplyDeleteYou're welcome!
DeleteThis works. Thank you. It appears the comma after 'reader' is optional.
ReplyDeleteGood point! Doesn't actually need to be there, I'll remove it.
Delete