title: Google Script Lock Cells
body: <p>I am trying to implement a script that locks a certain range of cells when a condition is true.  Here is the link to my document:</p>
  
  <p><a href="https://docs.google.com/spreadsheets/d/1XShGxlz2fA2w2omth-TvYc7cK0nXvVMrhwRKafzVjOA/edit?usp=sharing" rel="nofollow">https://docs.google.com/spreadsheets/d/1XShGxlz2fA2w2omth-TvYc7cK0nXvVMrhwRKafzVjOA/edit?usp=sharing</a></p>
  
  <p>Basically I am sharing this document with a group of people so they fill their mail addresses in column B and put a number 1 in column C so it increments my counters for each slot. What I am trying to do is to lock each slot when it is full so other people can no more edit these slots but the problem is with my if statement <code>if (cell1 == 10)</code>. The range is always locked even if the if condition is not realized. Here is my code : </p>
  
  <pre><code>function onOpen() {
  
  
    var ss = SpreadsheetApp.getActive();
    var source =         SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  
   var cell=60;
   var cell1 = source.getRange("G2").getValue();
  
   if (cell1 == 10){
  
  // Protect range B2:B11, then remove all other users from the list of editors.
  var ss = SpreadsheetApp.getActive();
  var range = ss.getRange('B2:B11');
  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);
  }
  
    }
  
  
  }
  </code></pre>