Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protect ranges with google apps script

I have a number of sheets which I need to protect except for certain ranges.

Is it possible to do this with a script, as I have several sheets, and many ranges within the sheet which I need to be unprotected so staff can edit the sheets.

The ranges I need to leave unprotected are repetitive, so I'm hoping it's doable. I will fill the ranges I want to remain UNPROTECTED with YELLOW on the example sheet I'll give you.

An example of one of the sheets can be viewed here.

As mentioned, I need to protect the whole sheet, apart from these ranges...

N4:V26,N30:V52,N56:V78 etc etc. Basically the rest of the sheet needs to be protected. The unprotected ranges, the columns stay the same, but each unprotected range is separated by three rows which are protected.

If anyone can help with this script I'd be very grateful as it will save me hours of time manually protecting these ranges over many sheets.

Regards Matt

like image 857
Matt Avatar asked Aug 17 '16 10:08

Matt


People also ask

Can you lock a range in Google Sheets?

You can lock some of the cells while still allowing collaborators to make changes to others. Start by identifying and selecting the cells that you want to lock. Open the Data menu at the top of the document, and then select Protected Sheets and Ranges in the dropdown menu.

Why can't I protect range in Google Sheets?

You must give another user permission to edit the whole spreadsheet before you can protect a range. For example, if you want someone to only be able to edit column B, you would need to first give them permission to edit the spreadsheet, and then follow the procedure below to protect all the other columns.


1 Answers

Yes, you can accomplish this using the Protection class. You would first protect the whole sheet using var protection = sheet.protect(), then unprotect the ranges you want people to be able to edit using protection.setUnprotectedRanges([ranges]), where [ranges] is an array of range objects. You can read more about it in the Google Apps Script Class Protection documentation.

like image 166
Tiffany G. Wilson Avatar answered Oct 12 '22 04:10

Tiffany G. Wilson