🍄Protection

// 1. get range A1:B10, 
var ss = SpreadsheetApp.getActive();
var range = ss.getRange('A1:B10');

// 2. protect range
var protection = range.protect().setDescription('Sample protected range');

// 3. ensure current user is editor before removing. 
//    (Otherwise, if the user's edit permission comes from a group, 
//     the script throws an exception upon removing the group.)
var me = Session.getEffectiveUser();
protection.addEditor(me);

// 4. then remove all other users from the list of editors.
protection.removeEditors(protection.getEditors());

if (protection.canDomainEdit()) {
  protection.setDomainEdit(false);
}

Last updated