Here is the script in the table that runs the editor:

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 removeEditor (), also does not work: If this user has already been a list of editors, this method has no effect .

On the other hand, through the table interface the editor can lower its access rights to the Viewer.

Please advise how to properly lower access rights.

UPD Solution found, thanks, @oshilaer. The original error "ReferenceError: The object" Drive "was not defined" was caused by the fact that the Drive API was not enabled: Script Editor: Resources / Additional functions in the Google API console .

  • This code works. You have a mistake elsewhere. - oshliaer
  • Does the @oshliaer code work when run as an editor? I need exactly this. - Boris Baublys 4:38 pm
  • @oshliaer here link Maybe you make a request? I will give you the right to edit. Fix the script the way you think it should work and check it out. - Boris Baublys pm

1 answer 1

The main task: to make downgrade rights to the editor himself.

No need to manipulate users and access through a spreadsheet file. These are mediated methods that are implemented only thanks to the general interface of the Disk file type.

If you use Advanced Drive Service, then everything works like a clock.

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

The editor transmits his email and changes his rights to the reader or commentator.

Snippet

  • @oshilaer Thank you, this is a great thought - "No need to manipulate users and access through a spreadsheet file ... use the Advanced Drive Service"! But the code gives the error "ReferenceError: The object" Drive "is not defined. (Line 18, ed2view file)". - Boris Baublys