简体   繁体   中英

Google scripts for cell protection at a specific time

I need help to write a script. Here's what I want to do: protect specific cells according to a programmed time schedule. I am able to protect the cells according to a time limit. I have difficulty changing cells every day for the protection. For example, I would like to protect: A1:B5 (day 1), C1:D5 (day 2), A6:B10 (day 3), C6:D10 (day 4). I don't know how to do that.

Here is a script that I found and that works well for me, but I need another section for different cell.

function AddProtectionToColumn() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var range = ss.getRange('A1:B5');
  var protectSs = range.protect().setDescription('Protect');  
  var me = Session.getEffectiveUser();
  protectSs.addEditor(me);
  protectSs.removeEditors(protectSs.getEditors());
  if (protectSs.canDomainEdit()) {
    protectSs.setDomainEdit(false); 
  }
}

Also, I don't have much experience with scripting and my first language is French. I hope I have been clear in my explanations.

You can create a second function that sets the range you want based on the day of the week and call that from you current function:

function AddProtectionToColumn() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var range = GetRange(ss);
  var protectSs = range.protect().setDescription('Protect');
  var me = Session.getEffectiveUser();
  protectSs.addEditor(me);
  protectSs.removeEditors(protectSs.getEditors());
  if (protectSs.canDomainEdit()) {
    protectSs.setDomainEdit(false); 
  }
}

function GetRange(ss){
  var today = new Date().getDay();
  // assuming you're only making protected ranges on the first sheet
  var protections = ss.getSheets()[0].getProtections(SpreadsheetApp.ProtectionType.RANGE); 
  if (today == 0){ // sunday
    //assuming you want to remove yesterday's protection
    removeProtections(protections);
    return ss.getRange('range-for-sunday');
  }
  else if (today == 1){ // monday
    removeProtections(protections);
    return ss.getRange('range-for-monday');
  }
  else if (today == 2){ // tuesday
    removeProtections(protections);
    return ss.getRange('range-for-tuesday');
  }
  else if (today == 3){ // wednesday
    removeProtections(protections);
    return ss.getRange('range-for-wednesday');
  }
  else if (today == 4){ // thursday
    removeProtections(protections);
    return ss.getRange('range-for-thursday');
  }
  else if (today == 5){ // friday
    removeProtections(protections);
    return ss.getRange('range-for-friday');
  }
  else if (today == 6){ // saturday
    removeProtections(protections);
    return ss.getRange('range-for-saturday');
  }
}

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

The removeProtections() function I added in case you want to remove the protection from the day before, you can remove it if you want to keep them.

References:

 function AddProtectionToColumn() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var range = GetRange(ss);
      var protectSs = range.protect().setDescription('Protect');
      var me = Session.getEffectiveUser();
      protectSs.addEditor(me);
      protectSs.removeEditors(protectSs.getEditors());
      if (protectSs.canDomainEdit()) {
        protectSs.setDomainEdit(false); 
      }
    }

    function GetRange(ss){
      var today = new Date().getDay();
      // assuming you're only making protected ranges on the first sheet
       var protections = ss.getSheets()[0].getProtections(SpreadsheetApp.ProtectionType.RANGE);
      if (today == 0){ // sunday
        return ss.getRange('A1:B5');
      }
      else if (today == 1){ // monday
        return ss.getRange('C1:D5');
      }
      else if (today == 2){ // tuesday
        return ss.getRange('A6:B10');
      }
      else if (today == 3){ // wednesday
        return ss.getRange('C6:D10');
      }
      else if (today == 4){ // thursday
        return ss.getRange('A7:B11');
      }
      else if (today == 5){ // friday
        return ss.getRange('C7:D11');
      }
      else if (today == 6){ // saturday
        return ss.getRange('A8:B12');
      }
    }

Hello. This is the final version that work very well! Thanks to Rafa Guillermo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM