Skip to content Skip to sidebar Skip to footer

Automatically Locking A Range (column) As Each Date Passes?

I have a sheet that employees will update daily with information about tasks done that day. Each column has a date in the header row (row 3 in this case), and after the end of the

Solution 1:

Yes, there is a way to do this.

I will briefly describe the solution:

  1. Let's say that the first row has 1:1 contains consecutive dates.
  2. Create function lockColumns which would create new protected range.
  3. Add function lockColumns to time trigger, which triggers every day between 0:01 and 1:00 am.

And now some code:

functionlockColumns() {
  var ss = SpreadsheetApp.getActive().getSheetByName('Sheet 1')
  var range = ss.getRange('1:1').getValues()[0];
  var today = newDate();
  var todayCol = null;
  for (var i=0; i<range.length; i++) {       
    if (today.isSameDateAs(range[i])) {
      todayCol = i;
      break;
    }
  } 

  var rangeToProtect = ss.getRange(1, todayCol +1, ss.getMaxRows(), 1)
  var protection = rangeToProtect.protect().setDescription('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);
  }
  protection.addEditor('email@gmail.com'); // second person with edit permissions
}

/*
http://stackoverflow.com/a/4428396/2351523
*/Date.prototype.isSameDateAs = function(pDate) {
  return (
    this.getFullYear() === pDate.getFullYear() &&
    this.getMonth() === pDate.getMonth() &&
    this.getDate() === pDate.getDate()
  );
}

Post a Comment for "Automatically Locking A Range (column) As Each Date Passes?"