shivashankar G
shivashankar G

Reputation: 11

Protect cells until some condition is met

In Apps script, I am trying to protect few cells until some condition is met enter image description here

Please refer above image, if F2 is Done, then remove protection from A2:E2 or protect from editing.

Please help with the script, I want this script to every row.

 function onEdit() 

  var ss = SpreadsheetApp.getActive();
  var source = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SheetName");
  var cell = source.getRange("F2").getValue();
  var range = ss.getRange('A2:E2');

  if (cell == Done) {

    
    var protection = range.protect().setDescription('Sample protected range');
    Logger.log

  } else {

    
    var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);

    for (var i = 0; i < protections.length; i++) {
      var protection = protections[i];
      protection.remove();
    }
  } 
} 

Upvotes: 1

Views: 61

Answers (1)

doubleunary
doubleunary

Reputation: 18786

The Range.protect() method requires authorization, so you cannot run it from an onEdit(e) simple trigger. Use an installable trigger instead.

Upvotes: 1

Related Questions