Skip to content
Advertisement

Google Sheets Script to lock or unlock a row depending on the value in the cell of a column

I use a script that allows me to lock a row if in the “BG” column I put an “X”.

I would like that if I put a “0” then the protection is removed.

Also add a function so that the script runs automatically when I change the value in the “BG” cell.

My Script:

JavaScript

Advertisement

Answer

Instead of looping through all the rows, just use onEdit Trigger. onEdit Trigger will execute a function whenever a user edited or inserted value to the sheet. It has an Event Object which has property of range which you can use to determine the row and column of the edited cell. Using those properties you can easily lock a specific row.

Try this code:

JavaScript

Demo:

Test Sheet:

enter image description here

Adding “X”:

enter image description here

Replacing “X” with “O”:

enter image description here

References:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement