首页 文章

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

提问于
浏览
0

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

今天需要输入并对条目进行更改 .

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

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

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

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

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

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

提前谢谢你的帮助!

昆士兰镍

1 回答

  • 0

    Protect Sheet except for today's rows

    我从pkowalczyk获取了您链接到的代码并修改它以保护整个工作表,除了今天的行 . 大部分代码也可以在文档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
    }
    

    本节来自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()
      );
    }
    

    这是一个有趣的问题,因为我没有非常使用保护 . 我发现在运行代码时让Protected Sheets和Ranges侧边栏向上移动很方便 . 我强调了受保护和未受保护的范围,只是为了清楚它们是什么 .

    Trying to unProtect more than one row

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

    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
    }
    

相关问题