Is it possible to hand over the owner permission to the other user and change the former owner to a viewer using apps script?
Transferring the owner permission to another user and being a viewer
I'm trying to make a script that hands over the owner to another user and changes the former owner to a viewer or prohibits the former owner from editing the Google Spreadsheet. The problem is the script is run by the former owner and the script cannot remove the edit permission of the former owner.
What I have tried
I tried setViewer(), sheet.protect(), and contentRestictions.readOnly but all of them was not a viable solution
removeEditors()
andsetViewer()
setViewer()
method to an editors has no effects, and after applying removeEditors() to the former owner(after changing the owner, of course) the script cannot execute setViewer()
since it does not have permission anymore.
sheet.protect()
the method gives the permission to edit the protected range for the owner and the user who runs the script. not eligible.
contentRestrictions.readOnly
the restriction can be unlocked by editors. not feasible.
Code for contentRestrictions:
function setReadOnly() { var ss = SpreadsheetApp.getActive(); var supervisor = 'xxxxxxxx@gmail.com'; file = DriveApp.getFileById(ss.getId()); try { Drive.Files.update({ 'writersCanShare': false, 'copyRequiresWriterPermission': true, 'contentRestrictions': [{ 'readOnly': true }] }, ss.getId()); file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.NONE); file.setOwner(supervisor); } catch (e) { console.log(e); } }
I recently found the contentRestrictions.readOnly
has been implemented to Google Drive API. While I thought it would be a great way to restrict the editors from modifying the file content, I realized the users who are editors can "UNLOCK" the file with a click even it was locked by the owner.
I don't understand the use of this property if the restriction can be resolved by editors. The explanation in the docs says that we can use this field to prevent modifications to the title, uploading a new revision, and addition of comments. However, editors easily unlock the files, and the viewers cannot modify the file anyway.