![](/img/trans.png)
[英]How can I lock editing permissions in Google Sheets using Google Scripts, based on cell colour?
[英]How can I lock editing permissions in Google Sheets using Google Scripts, based on cell color AND continent?
我可以按颜色锁定,但也需要只锁定包含任何内容的白色单元格(ffffff)。
var ss = SpreadsheetApp.getActiveSpreadsheet(); //clears green feilds
var sheet = ss.getSheetByName("Sheet1");
var color = sheet.getRange('C23').getBackground()
var range = sheet.getDataRange();
var bgColors = range.getBackgrounds();
var data = range.getValues();
var formulas = range.getFormulas(); // Added
for (var i = 0; i < bgColors.length; i++) {
for (var j = 0; j < bgColors[i].length; j++) {
if (bgColors[i][j] == color) {
data[i][j] = '';
}
}`enter code here`
}
formulas.forEach((r, i) => r.forEach((c, j) => { // Added
if (c) data[i][j] = c;
}));
range.setValues(data).setBackgrounds(bgColors);//reset values and backgrounds
如果我理解正确,您需要保护与单元格 C23 具有相同颜色的单元格以及白色但没有空值的单元格。 如果是这样,你可以尝试这样的事情:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var color = sheet.getRange('C23').getBackground()
var range = sheet.getDataRange();
var bgColors = range.getBackgrounds();
var data = range.getValues();
var formulas = range.getFormulas(); // Added
for(var i=0; i< bgColors.length; i++)
{
for(var j=0; j < bgColors[i].length; j++)
{
if((bgColors[i][j] == color)||(bgColors[i][j] == "#ffffff" && data[i][j] != "" ))
{
var protection = range.getCell(i+1, j+1).protect();
var editors = protection.getEditors();
protection.removeEditors(editors);
//protects the cell that matches the criteria and removes any editor
//so that only you have access to edit it
}
}
}
formulas.forEach((r, i) => r.forEach((c, j) => { // Added
if (c) data[i][j] = c;
}));
range.setValues(data).setBackgrounds(bgColors);//reset values and backgrounds
}
让我知道这是否适合您,或者您使用的标准是否不同,以便我可以更改脚本。
参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.