当列中的日期大于24小时时锁定工作表:Google表格-Scripteditor?

时间:2018-10-26 14:41:05

标签: google-apps-script google-sheets locking

为了防止其他贡献者造成数据丢失,我想为今天的所有数据锁定整个工作表。

今天有必要进行输入和更改条目。

主文件的简单示例:EXAMPLE - LOCK < TODAY

因此,当A列中的日期<今天时,每一行都将锁定其他行。

这个link 使我离我越来越近,但是我遇到了

  

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

在第31行给我一个错误:“ TYPE-ERROR:在对象中找不到函数getFullYear ...”

打开其他任何想法/代码。

预先感谢您对我的帮助!

Qni

1 个答案:

答案 0 :(得分:0)

保护表(今天的行除外)

我从pkowalczyk中获取了您链接到的代码,并对其进行了修改,以保护整个表(今天的行除外)。 here文档中也提供了许多此类代码。

preg_replace

本节来自Incidently,您可以参考它以获取更多说明。 “ this”是指代码中的今天,它只比较年,月和日。

//replaces only free numbers
$str = 'THIS IS ALSO A STRING      123';
echo preg_replace('/(\s+\d+)?$/', '', $str); 
//output: 'THIS IS ALSO A STRING'

//does not trim
$str = '   THIS IS ALSO A STRING      123   ';
echo preg_replace('/(\s+\d+)?$/', '', $str); 
//output: '   THIS IS ALSO A STRING   '

//does NOT strip numbers from the end of a word
$str = 'THIS IS ALSO A STRING123';
echo preg_replace('/(\s+\d+)?$/', '', $str); 
//output: 'THIS IS ALSO A STRING123'

//same as above except DOES strip numbers from the end of a word
$str = 'THIS IS ALSO A STRING123';
echo preg_replace('/(\s+\d+)?$/', '', $str); 
//output: 'THIS IS ALSO A STRING'

//same as the last one except trims both sides
$str = '   THIS IS ALSO A STRING123   ';
echo preg_replace('^\s*|\d*$|\s*$', '', $str); 
//output: 'THIS IS ALSO A STRING'

//using preg_match
$str = 'THIS IS ALSO A STRING   123';
echo preg_match('/(.+?)(\s*\d+)?$/', $str, $match); 
echo $match[1];
//output: 'THIS IS ALSO A STRING'
echo $match[2];
//output: '   123'

//same as above except also captures the numbers without whitespace
//wont match 'THIS IS ALSO A STRING   123  ' <-- ending white space
$str = 'THIS IS ALSO A STRING   123';
echo preg_match('/(.+?)(?:\s*(\d+))?$/', $str, $match); 
echo $match[1];
//output: 'THIS IS ALSO A STRING'
echo $match[2];
//output: '123'

//same as above except adds something like trimming
$str = '  THIS IS ALSO A STRING   123  ';
echo preg_match('/\s*(.+?)(?:\s*(\d+))?\s*$/', $str, $match); 
echo $match[1];
//output: 'THIS IS ALSO A STRING'
echo $match[2];
//output: '123'
  
    

这是一个有趣的问题,因为我没有非常多地使用保护功能。我发现在运行代码时将“受保护的图纸和范围”侧栏打开起来非常方便。我突出显示了受保护和不受保护的范围,只是为了清楚它们是什么。

  

试图取消保护多行

这应该有所帮助。它会收集所有范围以在阵列中取消保护,并一次取消所有保护。

//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
}