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

 

No comments:

Post a Comment