繁体   English   中英

如何根据单元格颜色使用 Google 脚本锁定 Google 表格中的编辑权限?

[英]How can I lock editing permissions in Google Sheets using Google Scripts, based on cell colour?

我正在使用几个颜色编码的 Google 表格,并试图锁定特定颜色(如粉红色)的所有单元格。 这个想法是因为它们包含的公式而自动填充这些单元格,但任何人都不能编辑。

我是 Google Scripts 的超级新手,我正在尝试使用 ClassProtection。 但如果有人已经有实施类似解决方案的经验,我将非常感谢您的建议。 我想我必须将 go 放入每个工作表中,并集成相同的 Google 脚本,以便所有工作表上的所有粉红色单元格都以类似的方式锁定。

先感谢您!

以下脚本可以保护某些范围,您可以根据需要将各种工作表中的范围定义到 select。

 function script() { var spreadsheet = SpreadsheetApp.getActive(); var lastrow = spreadsheet.getLastRow(); var lastcolumn = spreadsheet.getLastRow(); var ranges = spreadsheet.getRange(1, 1, lastrow, lastcolumn).getBackground() var protection = spreadsheet.getActiveSheet().protect(); for (var i = 0; i < ranges.length; i++) { for (var j = 0; j < lastcolumn; j++) { if (ranges[i][j] == 'Pink') { //any color code protection.setUnprotectedRanges([spreadsheet.getRange(i, j)]).removeEditors(['user1@domain.com', 'user2@domain.com']); protection.addEditor('user0@domain.com'); } } } SpreadsheetApp.flush(); };

问题:

您想保护电子表格中具有特定颜色的所有单元格。

解决方案:

您可以执行以下操作:

  • 使用Spreadsheet.getSheets()forEach()遍历电子表格中的所有工作表。
  • 对于每个工作表,您必须遍历工作表中的所有单元格,而不仅仅是具有 content的单元格,因为单元格可以是空的并且具有您想要保护的颜色。 因此,您需要知道每张工作表中的最后一行和最后一列是什么,而不仅仅是包含 content的最后一行和最后一列。 要检索此信息,您可以使用Sheet.getMaxRows()Sheet.getMaxColumns() (如果您不需要使用内容保护最后一行/列之后的单元格,您可以使用getLastRow()getLastColumn()代替) .
  • 一旦知道要为每个工作表获取的范围的尺寸,就可以使用getRange(row, column, numRows, numColumns)来检索相应的范围,并使用getBackgrounds()来检索范围内每个单元格的十六进制颜色代码,在一个二维数组。
  • 在检索到具有背景 colors(在下面的示例中称为backgrounds )的二维数组后,您必须遍历该数组。 下面的示例中使用了两个for循环。
  • 对于每个单元格,您必须检查背景颜色是否与您要保护的颜色代码匹配。 在下面的示例中,代码为#ff00ff ,对应于命名的magenta 如果这不是您要保护的颜色,请在代码中进行编辑。
  • 如果颜色匹配,脚本将保护单元格。 这可以通过首先检索单元格,使用for循环计数器索引 ( i , j ) 和getRange(row, column)来完成,然后调用Range.protect()并删除所有编辑器,但运行脚本的用户(您无法保护该用户的范围),如我刚刚链接的方法参考所示。

代码片段:

function protectColor() {
  const spreadsheet = SpreadsheetApp.getActive();
  const sheets = spreadsheet.getSheets();
  sheets.forEach(sheet => { // Loop through sheets in spreadsheet
    const maxRow = sheet.getMaxRows(); // Get last row in sheet
    const maxColumn = sheet.getMaxColumns(); // Get last column in sheet
    const backgrounds = sheet.getRange(1,1,maxRow,maxColumn).getBackgrounds(); // Get cell colors
    for (let i = 0; i < backgrounds.length; i++) { // Loop through rows in sheet
      for (let j = 0; j < backgrounds[i].length; j++) { // Loop through current row
        if (backgrounds[i][j] === "#ff00ff") { // Change if necessary
          const pinkCell = sheet.getRange(i+1, j+1); // Get pink cell
          const protection = pinkCell.protect(); // Protect cell
          const me = Session.getEffectiveUser();
          protection.addEditor(me);
          protection.removeEditors(protection.getEditors());
          if (protection.canDomainEdit()) {
            protection.setDomainEdit(false);
          }
        }
      }
    }
  });
}

笔记:

  • 由于完全不知道哪些单元格是粉红色的,因此脚本必须用这种颜色单独保护每个单元格。 这不是很有效,如果您知道单元格着色有一种模式(例如,如果一行中的所有单元格始终具有相同的颜色),您可以一次着色更大的范围,减少protect的次数进程应该运行,并提高效率(请参阅使用批处理操作)。

暂无
暂无

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

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