Tuesday 28 December 2021

Change file permissions from Editor to Viewer

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);

 

4 comments:

  1. Thanks so much! it helps me a lot.

    ReplyDelete
  2. This works. Thank you. It appears the comma after 'reader' is optional.

    ReplyDelete
    Replies
    1. Good point! Doesn't actually need to be there, I'll remove it.

      Delete