How to change access rights to a spreadsheet from Editor to Viewer?

How to change access rights to a spreadsheet from Editor to Viewer?



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.




2 Answers
2



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


User


string


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.






Thank you, @JSmith, you are right about String! However, the script does not work anyway. The editor loses all rights. Maybe after removeEditor() at once ActiveSpreadsheet is deleted and accordingly there is no ss? Maybe I need to first save the spreadsheet ID to a variable and then go to addViewer() by id?

– Boris Baublys
Sep 17 '18 at 2:56







@BorisBaublys can you share the spreasheet please?

– JSmith
Sep 17 '18 at 9:55






Oh sure. Please write your email - I will give you the right to edit.

– Boris Baublys
Sep 17 '18 at 16:04






@BorisBaublys just share the link please I'll send you a request

– JSmith
Sep 17 '18 at 16:11







@BorisBaublys as told in the post, the code you gave me works if you are another editor or the owner of the sheet. But an editor cannot programmatically loose all his right and continue executing your function to change his status this is out of logic, this would be a security issue.

– JSmith
Sep 17 '18 at 18:20



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






are you sure the editor can use that code, didn't worked for me.

– JSmith
Sep 21 '18 at 12:32






@JSmith The code works, I checked. What error do you get?

– Boris Baublys
Sep 22 '18 at 16:18






it works if you are the owner but as an editor I get Project 46022463242 is not found and cannot be used for API calls. If it is recently created, enable Drive API by visiting https://console.developers.google.com/apis/api/drive.googleapis.com/overview?project=46022463242 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry. (line 32, file "ed2view")

– JSmith
Sep 22 '18 at 16:43


Project 46022463242 is not found and cannot be used for API calls. If it is recently created, enable Drive API by visiting https://console.developers.google.com/apis/api/drive.googleapis.com/overview?project=46022463242 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry. (line 32, file "ed2view")






@oshliaer I've tried directly on the script linked to the spreadsheet that why I get this code. I'll try on an unlinked script.

– JSmith
Sep 22 '18 at 20:58







@BorisBaublys right that works good job. but it doesn't work from the script linked to the spreadsheet

– JSmith
Sep 22 '18 at 21:02




Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)