我在 Google Docs 中有一个脚本,如果 Status 列包含某个值,它会设置行的背景颜色。问题是工作表具有某些受保护的列,因此当受限制的用户在其部分触发脚本时,它会运行,但他们会收到令人讨厌的权限错误消息(因为脚本会为受保护列的部分着色)。因此,我需要创建 2 个脚本,以块的形式为行着色,因此当受限用户触发个性化状态消息时,只有其未受保护的列着色。这是为整行着色的代码(由可安装的 onedit 触发器触发):
function setBackLogColor() {
var range = SpreadsheetApp.getActiveSheet().getDataRange();
var statusColumnOffset = getStatusColumnOffset();
for (var i = range.getRow(); i < range.getLastRow(); i++) {
rowRange = range.offset(i, 0, 1);
status = rowRange.offset(0, statusColumnOffset).getValue();
if (status == 'TO LD')
{
rowRange.setBackgroundColor("#cfe2f3");
}
else if (status == 'TO GB' )
{
rowRange.setBackgroundColor("#d9ead3");
}
else if (status == 'TO OUTSIDE PARTY - WILL COME BACK' )
{
rowRange.setBackgroundColor("#f4cccc");
}
else if (status == 'Hand Delivery 2' )
{
rowRange.setBackgroundColor("#d9ead3");
}
else if (status == 'Hand Delivery' )
{
rowRange.setBackgroundColor("#cfe2f3");
}
else
{
rowRange.setBackgroundColor("#FFFFFF");
}
}
}
//Returns the offset value of the column titled "Status"
//(eg, if the 7th column is labeled "Status", this function returns 6)
function getStatusColumnOffset() {
lastColumn = SpreadsheetApp.getActiveSheet().getLastColumn();
var range = SpreadsheetApp.getActiveSheet().getRange(1,1,1,lastColumn);
for (var i = 0; i < range.getLastColumn(); i++) {
if (range.offset(0, i, 1, 1).getValue() == "Status") {
return i;
}
}
}
任何帮助将不胜感激!谢谢!