如何保护一张纸然后取消保护特定的单元格 [英] How to protect a sheet then unprotect specific cells

查看:85
本文介绍了如何保护一张纸然后取消保护特定的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个在打开Goog​​leSheet时运行的脚本.该脚本取消保护整个工作表,然后保护除当前日期的行以外的所有内容.该脚本完全可以实现我想要的功能,但是在我看来,它太麻烦且耗时.

I have created a script that runs on opening of a GoogleSheet. The script unprotects the entire sheet then protects all but the row of the current date. The script does exactly what I want, but it occurred to me that it is too cumbersome and time consuming.

除了保护整个特定范围之外,还有一种方法可以保护整个工作表,然后仅取消保护几个范围.就脚本运行时间而言,这似乎更为有效.

Instead of protecting a whole bunch of specific ranges, is there a way to protect the entire sheet and then unprotect only a few ranges. This would seem to be much more efficient regarding script run time.

我唯一的问题是,当我尝试取消保护特定范围时,代码将引发错误.

My only problem is that when I try to unprotect specific ranges, the code throws an error.

Cannot find method setUnprotectedRanges(Range)

此处的代码段:

var sheet = SpreadsheetApp.getActiveSheet();
    // Remove all range protections in the spreadsheet
    var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
    for (var i = 0; i < protections.length; i++) {
      var protection = protections[i];
      protection.remove();
    }

  var protection = sheet.protect();
    //restrict editors to owner
    protection.getRange().getA1Notation();
    var eds = protection.getEditors();
    protection.removeEditors(eds);

    //set unprotected ranges
    var range1 = sheet.getRange("A2:L2");
    protection.setUnprotectedRanges(range1);
    var range1 = sheet.getRange("A4:C4");
    protection.setUnprotectedRanges(range1); 

有什么建议吗?

加里

推荐答案

此修改如何?

  • 当添加不受保护的范围时,请将它们作为数组添加.

请如下修改//set unprotected ranges的部分.

//set unprotected ranges
var range1 = sheet.getRange("A2:L2");
var range2 = sheet.getRange("A4:C4");
protection.setUnprotectedRanges([range1, range2]);

如果要将新范围添加到现有不受保护的范围,请使用以下脚本.

If you want to add new ranges to the existing unprotected ranges, please use the following script.

var ranges = protection.getUnprotectedRanges();
var range1 = sheet.getRange("A2:L2");
var range2 = sheet.getRange("A4:C4");
ranges.push(range1);
ranges.push(range2);
protection.setUnprotectedRanges(ranges);

参考:

  • setUnprotectedRanges(范围)
  • Reference:

    • setUnprotectedRanges(ranges)
    • 如果这不是您想要的,请告诉我.我想修改它.

      If this was not what you want, please tell me. I would like to modify it.

      这篇关于如何保护一张纸然后取消保护特定的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆