Lock a sheet when date in column > 24 hours: Google Sheets - Scripteditor?

狂风中的少年 提交于 2020-06-01 07:12:52

问题


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


回答1:


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
}


来源:https://stackoverflow.com/questions/53011123/lock-a-sheet-when-date-in-column-24-hours-google-sheets-scripteditor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!