【问题标题】:Google Apps Script Range Protection Lock Tab color and Tab RenamingGoogle Apps 脚本范围保护锁定选项卡颜色和选项卡重命名
【发布时间】:2022-01-24 08:00:54
【问题描述】:

具有多个标签的电子表格有 10 个编辑者/访问权限。我想锁定其中 8 个编辑器在每个选项卡中的一些范围。

当我运行下面编写的 google app 脚本时,它还锁定了 8 个编辑器,无法编辑选项卡名称甚至选项卡颜色。可以告诉我哪里出错了吗?我希望他们仍然能够更改选项卡的名称和颜色。

function addClassProtectionFor_Current(){ //Main function to run

  var currentclasstab = SpreadsheetApp.getActiveSheet();

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

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

  //Add Editors to give access to the protected ranges
  protection.addEditors(["me.gmail.com","you@gmail.com"]);

  //set unprotected ranges
  var ranges = protection.getUnprotectedRanges();

  //Ranges to leave unlocked
  var data = ["A5:V19","B23:V26","B29:V35","B39:V45"];

  data.forEach(res => { //LOOPS INTO EVERY ARRAY CONTAINING SPECIFIC RANGES
    ranges.push(currentclasstab.getRange(res));
    protection.setUnprotectedRanges(ranges); //REMOVES THE PROTECTION ON THE RANGE
  });
}

【问题讨论】:

  • 我必须为我糟糕的英语水平道歉。不幸的是,我无法理解你的问题。您想从用户那里锁定您的 Google 电子表格中的哪些内容?
  • 我想保护除 A5:V19,B23:V26,B29:V35,B39:V45 范围之外的所有单元格,来自 8 个用户。其他 2 个用户可以访问所有内容。我的谷歌脚本就是这样做的,但它也阻止了 8 个用户更改选项卡名称和颜色。如何保护工作表(某些单元格除外)但仍允许 8 个用户更改选项卡名称或更改选项卡颜色。
  • 感谢您的回复。我不得不再次为我糟糕的英语水平道歉。对于您的回复,我想确认我的理解。在您的目标中,您希望锁定用户的单元格A5:V19,B23:V26,B29:V35,B39:V45,并且您希望锁定以更改用户的选项卡名称和选项卡颜色。我的理解正确吗?
  • 锁定:所有单元格。解锁:A5:V19,B23:V26,B29:V35,B39:V45 并允许所有用户编辑标签名称和颜色。
  • 感谢您的回复。根据您的回复,我提出了一个修改后的脚本作为答案。你能确认一下吗?如果我误解了您的问题并且没有用,我深表歉意。

标签: google-apps-script google-sheets protected


【解决方案1】:

我相信你的目标如下。

  • 您希望保护除A5:V19,B23:V26,B29:V35,B39:V45 的单元格之外的所有单元格,以防止用户访问。
  • 您想让用户在单元格受到保护的情况下更改选项卡名称和选项卡颜色。
  • 您希望使用 Google Apps 脚本实现此目的。

问题和解决方法:

不幸的是,在当前阶段,“UnprotectedRanges”似乎不能用于受保护的工作表。在这种情况下,用户无法更改选项卡名称和选项卡颜色。因此,在您的情况下,作为一种解决方法,为了使用户更改选项卡名称和选项卡颜色,需要保护除A5:V19,B23:V26,B29:V35,B39:V45 之外的所有单元格。当这反映在您的脚本中时,它变为如下。

修改脚本:

function addClassProtectionFor_Current() {
  var currentclasstab = SpreadsheetApp.getActiveSheet();
  var protections = currentclasstab.getProtections(SpreadsheetApp.ProtectionType.RANGE);
  for (var i = 0; i < protections.length; i++) {
    var protection = protections[i];
    protection.remove();
  }
  var ranges = ["A1:4", "W5:19", "A20:A", "B20:22", "B27:28", "B36:38", "W23:26", "W29:35", "W39:45", "B46:" + currentclasstab.getMaxRows()];
  currentclasstab.getRangeList(ranges).getRanges().forEach(r => {
    const protection = r.protect();
    var eds = protection.getEditors();
    protection.removeEditors(eds);
    protection.addEditors(["me.gmail.com","you@gmail.com"]);
  })
}
  • 在此脚本中,ranges 的值是除A5:V19,B23:V26,B29:V35,B39:V45 之外的所有单元格。如果您想保护其他单元格,请将它们添加到数组中。

参考:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 2010-09-27
    • 2018-03-26
    相关资源
    最近更新 更多