Reputation: 1203
Here is the script in the spreadsheet that the editor run:
function ed2view() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var eds = ss.getEditors();
var ed = eds[1];
ss.removeEditor(ed);
ss.addViewer(ed);
}
It does not work, because, apparently, by the time addViewer() the rights have already been revoked.
If just addViewer(), without the previous removeEditor(), also does not work: If the user was already on the list of editors, this method has no effect.
On the other hand, through the interface of the spreadsheet, the editor can lower its access rights to the Viewer.
Please advise how to correctly change access rights.
Upvotes: 1
Views: 3367
Reputation: 1203
The task has a solution if you do not manipulate access through the Spreadsheet file, but use the Advanced Drive Service.
function run() {
driveChangeMeRole_(FILE_ID, EMAIL);
}
/**
* Downgrade you in the rights from the editor to the viewer
* @param {string} fileId The file id
* @param {string} email You can pass your email and downgrade youself
* @returns {void}
*/
function driveChangeMeRole_(fileId, email) {
var permissionId = Drive.Permissions.getIdForEmail(email);
var resource = Drive.newPermission();
resource.role = 'reader';
/* You can stay as a commenter
resource.additionalRoles = ['commenter']; */
Drive.Permissions.update(resource, fileId, permissionId.id);
}
You need to enable the Drive API: Script Editor: Resources/Advanced Features and in Google API Console. The task was decided by @oshilaer here
Upvotes: 1
Reputation: 4810
try this if you want an editor to change other editor's status:
function ed2view() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var editorArray = ss.getEditors();
var userMail = editorArray[1].getEmail();
ss.removeEditor(editorArray[1]);
ss.addViewer(userMail);
}
In your code your trying to pass as argument a User
object instead of an email adress of type string
in addViewer()
if you are an editor and not the owner and you want to change your permission status with App Script well you just can't,still the editor can go into the share settings of the spreadsheet and change his permission status to 'read-only' then confirm.
Unfortunately you can't do that programmatically.
Upvotes: 0