【问题标题】:How can I lock editing permissions in Google Sheets using Google Scripts, based on cell colour?如何根据单元格颜色使用 Google 脚本锁定 Google 表格中的编辑权限?
【发布时间】:2020-12-15 09:40:06
【问题描述】:

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

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

提前谢谢你!

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 您只是检索工作表中第一个单元格的背景颜色(您应该改用getBackgrounds),这是行不通的。此外,您不会遍历电子表格中的所有工作表。此外,您正在保护整个工作表并取消保护粉红色单元格。这与OP想要的相反。最后,在脚本结束时调用flush 是没有意义的,脚本完成后工作表无论如何都会更新。
    【解决方案2】:

    问题:

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

    解决办法:

    您可以执行以下操作:

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

    代码sn-p:

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

    注意:

    • 由于完全不知道哪些单元格是粉红色的,脚本必须用这种颜色单独保护每个单元格。这不是很有效,如果您知道单元格着色有一种模式(例如,如果一行中的所有单元格始终具有相同的颜色),您可以一次为更大的范围着色,从而减少 @ 的次数987654340@进程应该运行,并提高效率(见Use batch operations)。

    【讨论】:

      猜你喜欢
      • 2019-07-15
      • 1970-01-01
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 2023-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多