0
votes

I am managing Google spreadsheets for 100+ schools. Each school's spreadsheet has multiple user roles (e.g., 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.

1
I would suggest doing a for loop all the files (if they're in the same folder). Then go through and programmatically set the ranges and protections and permissions for each of the documents. I can add some code, if it'll help more.cvnntg
Yes, if you could add some code that would be great.Chris
I understand how to do this for setting the protections of certain pages, but I did not think it was possible in Google Apps Script to protect ranges programmatically...Chris

1 Answers

0
votes

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