[英]How can I lock editing permissions in Google Sheets using Google Scripts, based on cell color AND continent?
[英]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(); };
您想保护电子表格中具有特定颜色的所有单元格。
您可以执行以下操作:
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.