Protecting Cells Based on Contents of Other Cells in Google Sheets -


i have single worksheet contains user entered responses in columns c & d, rows 3 - 20. responses time dependent , @ column e rows 3-20 see if "locked" or "open".

using protection, lock entire sheet editing exception of c3:d20. sheet set calculate every minute.

i trying write script checks column e see if set locked or open. if set locked, lock (protect) columns c&d in row editing myself. run script every 5 minutes , have loop , if statement handled, when go use removeeditors function 2 things:

  1. creates new protected range (so after 5 minutes have 1 additional protected range, 10 minutes, have 2 additional, etc.)
  2. does not remove other editors able edit cells.

i tried using google's example code, code adds current user editor, i'm trying avoid doing since editor can remove protection code putting in place.

any provide appreciated.

current code below:

function lock_cells() { var sheet = spreadsheetapp.getactive(); (var = 3; <= 20; i++) {   var check_cell = "e" + i;   var temp = sheet.getrange(check_cell).getvalue();   if (temp == "locked")   {      var lock_range = "c" + (i + 2) + ":d" + "i";      var protection = sheet.getrange(lock_range).protect();      var description = "row " + i;                       protection.setdescription(description);      var eds = protection.geteditors();      protection.removeeditors(eds);   } }   } 

to avoid creating new set of protected ranges, can add logic check rows locked. information need skip rows:

note: there mistake in line: var lock_range = "c" + (i + 2) + ":d" + "i"; variable should not have quotation.

function lock_cells() { var sheet = spreadsheetapp.getactive(); var rows = get_protected_rows();  (var =3; <= 20; i++) {   var check_cell = "e" + i;   var cell = sheet.getrange(check_cell);   var temp = sheet.getrange(check_cell).getvalue();   if (temp == "locked" &&  rows.indexof(i) <0)   {       var lock_range = "c" + + ":d" + i; //in line put "i"    ..... ... } function get_protected_rows() {   var ss = spreadsheetapp.getactive();   var protections = ss.getprotections(spreadsheetapp.protectiontype.range);   var rows = [];   (var = 0; < protections.length; i++) {    var protection = protections[i];    var anotation =   protection.getrange().getrow();    rows.push(anotation);   }    return rows } 

you right, when code executed 1 of users, protection gives user ability edit rows. recommend owner of file, run task remove every other editor rows. function similar previous. , know not best may use case.

function remove_editors() {   var ss = spreadsheetapp.getactive();   var protections = ss.getprotections(spreadsheetapp.protectiontype.range);   (var = 0; < protections.length; i++) {    var protection = protections[i];    var anotation =   protection.getrange().geta1notation();       var eds = protection.geteditors();       protection.removeeditors(eds);    }       } 

by doing able restrict permission other users. hope helps.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -