首页 > 解决方案 > 当列中的日期 > 24 小时时锁定工作表:Google 表格 - 脚本编辑器?

问题描述

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

今天需要可以对条目进行输入和更改。

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

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

这个链接让我更接近了,但我遇到了困难

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

这给了我第 31 行的错误:“TYPE-ERROR: can't find function getFullYear in object...”

对任何其他想法/代码开放。

预先感谢您帮助我!

Qni

标签: google-apps-scriptgoogle-sheetslocking

解决方案


保护工作表(今天的行除外)

我从pkowalczyk 获取了您链接到的代码并对其进行了修改以保护整个工作表,但今天的行除外。大部分代码也可在此处的文档中找到。

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

这部分来自Incidently,您可以参考它以获得更多解释。'this' 是指代码中的今天,它只是比较年、月和日。

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

这是一个有趣的问题,因为我没有太多使用保护。我发现在运行代码时启动受保护的工作表和范围侧边栏很方便。我强调了受保护和不受保护的范围,只是为了清楚它们是什么。

试图解除对多行的保护

这应该会有所帮助。它将所有要取消保护的范围收集到一个数组中,并一次取消保护。

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
}

推荐阅读