简体   繁体   English

Google 脚本锁定单元格

[英]Google Script Lock Cells

I am trying to implement a script that locks a certain range of cells when a condition is true.我正在尝试实现一个脚本,当条件为真时锁定特定范围的单元格。 Here is the link to my document:这是我的文档的链接:

https://docs.google.com/spreadsheets/d/1XShGxlz2fA2w2omth-TvYc7cK0nXvVMrhwRKafzVjOA/edit?usp=sharing https://docs.google.com/spreadsheets/d/1XShGxlz2fA2w2omth-TvYc7cK0nXvVMrhwRKafzVjOA/edit?usp=sharing

Basically I am sharing this document with a group of people so they fill their mail addresses in column B and put a number 1 in column C so it increments my counters for each slot.基本上,我与一群人共享此文档,因此他们在 B 列中填写他们的邮件地址,并在 C 列中放置一个数字 1,这样它会为每个插槽增加我的计数器。 What I am trying to do is to lock each slot when it is full so other people can no more edit these slots but the problem is with my if statement if (cell1 == 10) .我想要做的是在每个插槽已满时锁定每个插槽,以便其他人无法再编辑这些插槽,但问题在于我的 if 语句if (cell1 == 10) The range is always locked even if the if condition is not realized.即使没有实现 if 条件,范围也始终被锁定。 Here is my code :这是我的代码:

function onOpen() {


  var ss = SpreadsheetApp.getActive();
  var source =         SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");

 var cell=60;
 var cell1 = source.getRange("G2").getValue();

 if (cell1 == 10){

// Protect range B2:B11, then remove all other users from the list of editors.
var ss = SpreadsheetApp.getActive();
var range = ss.getRange('B2:B11');
var protection = range.protect().setDescription('Sample protected range');

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

  }


}

As Andy says in the comments, you need to explicitly remove the protection if cell G2 does not equal 10. (This code removes all protections).正如安迪在评论中所说,如果单元格 G2 不等于 10,您需要明确删除保护。(此代码删除所有保护)。

Reading the Protection Class page, where you got the snippets from, I couldn't get a handle on the way editor privileges would factor into your needs, so this script will work if others are editors.阅读保护类页面,您从中获得了片段,我无法了解编辑器权限将影响您的需求的方式,因此如果其他人是编辑器,此脚本将起作用。 If you don't want them to be editors, you'll have to add the relevant code.如果您不希望它们成为编辑器,则必须添加相关代码。

function onOpen() {

  var ss = SpreadsheetApp.getActive();
  var source = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var cell = source.getRange("G2").getValue();
  var range = ss.getRange('B2:B11');

  if (cell == 10) {

    // Protect range B2:B11 if cell 'G2' = 10
    var protection = range.protect().setDescription('Sample protected range');
    Logger.log

  } else {

    // Remove protection if cell 'G2' is anything other than 10
    var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);

    for (var i = 0; i < protections.length; i++) {
      var protection = protections[i];
      protection.remove();
    }
  }  
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM