简体   繁体   中英

Google Apps Script - Range Protection & Access

I am managing Google spreadsheets for 100+ schools. Each school's spreadsheet has multiple user roles (eg, principal, coach, teacher, etc.) for which I need to restrict access and only allow people to edit certain ranges. Programmatically protecting the ranges would be the easiest solution, but unfortunately, it is not possible to do that at this time with Google Apps Script.

Last year when I did this, I created a template that included all of the protected ranges. Then I copied the template 100+ times (which kept all of the range protections). Then I added users programmatically and gave them "Comment Only" access. Then I changed their access to "Can Edit" and manually gave them access to edit only the ranges that they needed. This process only gave them editing access to the ranges that I manually changed, while keeping them "Can comment" access for the other ranges (that I wanted protected). While tedious, this worked fine because there were a lot more ranges that were protected than editable for most users...so remaining as "Can comment" for the other ranges was convenient.

This year I am trying to do the same thing. I have protected the ranges on the spreadsheet and added my users with "Can comment" permissions. However, when I change the users to "Can edit" access, they can edit everything (including the protected ranges)! It seems that this is the default functionality. Now I have to go into each range and remove editing rights. Last year for each range I only had to grant editing rights for a small number of users...this is going to be much more time-consuming if I cannot find a work-around.

Does anyone know of a workaround for this? Is this a product of Google Spreadsheets 2.0 (last year I used the old version of Google Sheets)?

Thanks.

Since February 2015 , it is now possible to programmatically protect ranges and sheets using Google App Scripts. Note that the Protection class only works with the new Sheets .

Example from the documentation :

// Protect range A1:B10, then remove all other users from the list of editors.
 var ss = SpreadsheetApp.getActive();
 var range = ss.getRange('A1:B10');
 var protection = range.protect().setDescription('Sample protected range');

 // Ensure the current user is an editor before removing others. Otherwise, if the user's edit
 // permission comes from a group, the script will throw an exception upon removing the group.
 var me = Session.getEffectiveUser();
 protection.addEditor(me);
 protection.removeEditors(protection.getEditors());
 if (protection.canDomainEdit()) {
   protection.setDomainEdit(false);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM