在特定时间锁定Google表格中的单元格

时间:2015-07-25 09:33:14

标签: google-apps-script google-sheets

我想以某种方式自动锁定我的Google表格单元格,以便每天在特定时间自动锁定。我应该可以编辑它,但不能编辑我的合作者。

如何解决此问题?

在样本表( link here )中,您将看到某些日期(15/7 / 2015-17 / 7/2015)。我想对其进行编码,以便每天晚上10点锁定每个日期(例如A1:B6)。

1 个答案:

答案 0 :(得分:10)

作为@NightShadeQueen suggested,可以使用Time-driven triggersprotect()-method of the Class Range的组合。

  1. 打开工作表中的脚本编辑器(工具>脚本编辑器......
  2. 创建锁定范围的代码,可能如下所示:
  3. 如果您的工作表看起来不像样本表,则必须修改代码。特别是lockRanges() - 函数,它定义了应该保护的范围。目前,它从单元格A1开始,一次向下逐行7行,直到达到工作表的末尾。

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName('Sheet1'); //INSERT SHEET NAME HERE
    
    function lockRanges() {
      //First cell to lock
      var row = 1;
      var col = 1;
    
      // Get last row with data in sheet
      var lastRow = sheet.getLastRow();
    
      //Loop until last row in sheet is passed
      while(row <= lastRow){
        //Lock current cell
        lockRange(row, col);
    
        // Go to row 7 steps down from current (the next date)
        row = row + 7;
      }
    }
    
    
    
    function lockRange(row, col){
      var range = sheet.getRange(row, col);
    
      // Create protection object. Set description, anything you like.
      var protection = range.protect().setDescription('Protected, row ' + row);
    
     // Ensure the current user is an editor before removing others. Otherwise, if the user's edit
     // permission comes from a group, the script will throw an exception upon removing the group.
     var me = Session.getEffectiveUser();
     protection.addEditor(me);
     protection.removeEditors(protection.getEditors());
     if (protection.canDomainEdit()) {
       protection.setDomainEdit(false);
     }
    }
    
    1. 添加时间驱动的触发器
      1. 仍然在脚本编辑器中;转到资源&gt;当前项目的触发器
      2. 创建新触发器
      3. 选择Run = lockRanges
      4. 选择Events = Time-drivenDay timer10pm to 11pm(或随时随地)
    2.   

      注意:时间可能稍微随机(read more)

      就是这样,现在范围将在您选择时受到保护。与手动保护的范围一样,它们显示在数据&gt;中。受保护的工作表和范围

      有些不清楚?提问!

相关问题