0

In order to prevent data loss caused by other contributors, I'd like to lock an entire sheet for all data < today.

It needs to be possible to do input and make changes for entries today.

An simple example of the master file : EXAMPLE - LOCK < TODAY

So, each row will lock for others when the date in column A < today.

This link brought me closer but I'm having difficulties with

var range = ss.getRange('1:1').getValues()[0];

which gives me an error on line 31: "TYPE-ERROR: can't find function getFullYear in object..."

Open to any other idea/code.

Thank you in advance for helping me out!

Qni

QNI
  • 1

1 Answers1

0

Protect Sheet except for today's rows

I took the code you linked to from pkowalczyk and modified it to protect entire sheet except for the row that is for today. Much of this code is also available in the documentation here.

//https://developers.google.com/apps-script/reference/spreadsheet/protection#setUnprotectedRanges(Range)
//https://stackoverflow.com/a/43828395/7215091
function unlockTodaysRowFromSheetProtection() {
  var ss = SpreadsheetApp.getActive();
  var sh = ss.getSheetByName('ProtectRows');
  var protection=sh.protect().setDescription('Protected Sheet');
  protection.getRange().setBackground('#ffffff');
  var rg = sh.getRange(2,1,sh.getLastRow()-1,1);
  var vA = rg.getValues();
  var today = new Date();
  var todayRow = null;
  for (var i=0; i<vA.length; i++) {       
    if (today.isSameDateAs(vA[i][0])) {
      todayRow = i;
      break;
    }
  } 

  var rangeToUnProtect = sh.getRange(todayRow + 2,1,1,sh.getLastColumn());
  protection.setUnprotectedRanges([rangeToUnProtect]);
  protection.getRange().setBackground('#ffff00');//highlight protected range
  rangeToUnProtect.setBackground('#00ffff');//highlight unprotected range
  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
}

This section came from Incidently and you can refer to it for more explanation. The 'this' refers to today in the code and it just compares year,month and day.

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

This was an interesting question as I've not used protection very much. I found it handy to have the Protected Sheets and Ranges sidebar up while running the code. I highlighted the protected and unprotected ranges just to be clear what they are.

Trying to unProtect more than one row

This should help. It collects all of the ranges to unprotect in an array and unprotects all at one time.

function unprotectTodaysRowsFromSheetProtection() {
  var ss = SpreadsheetApp.getActive();
  var sh = ss.getSheetByName('ProtectRows');
  var protection=sh.protect().setDescription('Protected Sheet');
  protection.getRange().setBackground('#ffffff');
  var rg = sh.getRange(2,1,sh.getLastRow()-1,1);
  var vA = rg.getValues();
  var today = new Date();
  var uprgA=[];
  for (var i=0; i<vA.length; i++) {       
    if (today.isSameDateAs(vA[i][0])) {
      uprgA.push(sh.getRange(i + 2,1,1,sh.getLastColumn()))
    }
  } 
  protection.setUnprotectedRanges(uprgA);
  protection.getRange().setBackground('#ffff00');
  for(var i=0;i<uprgA.length;i++){
    uprgA[i].setBackground('#00ffff');
  }
  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
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Hi Cooper. Thank you for your feedback. I assume these are two different options. However, for both of them I receive a TypeError: Can't call null method protect. (line 4, file 'Code'). There is something with the **sh.protect().setDescription('Protected Sheet')** – QNI Oct 30 '18 at 13:32