[英]Trying to setBackgroundColor based off of a keyword in Google Apps Script
I'm trying to highlight a row in a table based off of the relevant keyword.我正在尝试根据相关关键字突出显示表格中的一行。 However, my attempts at setting the background color of my range of cells have not yielded any colors.但是,我尝试设置单元格范围的背景颜色并没有产生任何 colors。 I used loggers inside the if/else statement and they all worked, so I think I'm doing setBackgroundColor
wrong somehow.我在 if/else 语句中使用了记录器,它们都有效,所以我认为我在某种程度上做错了setBackgroundColor
。
function statusHighlighter(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetName = ss.getActiveSheet();
var sheetValues = ss.getDataRange().getValues();
sheetValues.forEach(function(row, index) {
var acctStatus = row[7];
var highlightRange = sheetName.getRange(index+1, 1, 1, sheetName.getLastColumn());
if(acctStatus === "Kept"){
highlightRange.setBackgroundColor('##fffec4');
Logger.log(acctStatus + " " + index);
} else if(acctStatus === "Suspended"){
highlightRange.setBackground('##cf4a4a');
} else if(acctStatus === "Deleted"){
highlightRange.setBackground('##6c75ad');
}
});
}
There is one issue in your code and a couple of optimizations that I would like to suggest.您的代码中有一个问题,我想提出一些优化建议。
The hex color codes must contain only one #
but you are using two ##
.十六进制颜色代码必须只包含一个#
但您使用的是两个##
。
You are iteratively using setBackground but instead you can create an array with colors inside the forEach
loop and then use setBackgrounds to set the colors in one go.您正在迭代地使用setBackground ,但您可以在forEach
循环中创建一个包含 colors 的数组,然后使用setBackgrounds在一个 Z34D1F91FB2E514B8576FAB1A75A8A 中设置 colors。 Have a look at best practices to see why.查看最佳实践以了解原因。
Since you have a particular column of interest, it makes sense to consider only one column instead of the full dataRange
.由于您有一个特定的列感兴趣,因此只考虑一列而不是完整的dataRange
是有意义的。 In the following script, acctStatusVals
considers only column 8
.在以下脚本中, acctStatusVals
仅考虑8
列。 I choose 8
because in your code you use the array index for 7
which is equivalent for column 8
.我选择8
是因为在您的代码中,您使用7
的数组索引,这相当于8
列。 Be careful on that.对此要小心。 In my code, 8
means column H
.在我的代码中, 8
表示H
列。 Adjust this to your needs.根据您的需要进行调整。 To convert the 2D array into 1D since we are using a single column, you can use flat .由于我们使用的是单列,因此要将 2D 数组转换为 1D,您可以使用flat 。
I use the following expression: Array(lastCol).fill('#6c75ad')
to create an array of lastCol
number of elements ( last column with content ) and fill it with the same value, because the goal is to color the full row with the same color value.我使用以下表达式: Array(lastCol).fill('#6c75ad')
创建一个包含lastCol
元素数的数组( 最后一列内容)并用相同的值填充它,因为目标是为整行着色具有相同的颜色值。
Be careful with the active sheet.小心活动表。 Make sure you select the right sheet before you run this code.在运行此代码之前,请确保 select 正确。 I would advice you to use ss.getSheetByName('Sheet1');
我建议你使用ss.getSheetByName('Sheet1');
but I leave it as a comment since this step is optional.但我将其作为评论留下,因为这一步是可选的。
function statusHighlighter(){
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheetName = ss.getActiveSheet();
// const sheetName = ss.getSheetByName('Sheet1'); // change to the name of your sheet
const lastRow = sheetName.getLastRow();
const lastCol = sheetName.getLastColumn();
const acctStatusVals = sheetName.getRange(1,8,lastRow).getValues().flat();
const colors = [];
acctStatusVals.forEach(acctStatus=>{
if(acctStatus === "Kept"){
colors.push(Array(lastCol).fill('#fffec4'));
} else if(acctStatus === "Suspended"){
colors.push(Array(lastCol).fill('#cf4a4a'));
} else if(acctStatus === "Deleted"){
colors.push(Array(lastCol).fill('#6c75ad'));
}
});
sheetName.getRange(1, 1, lastRow, lastCol).setBackgrounds(colors);
}
Sheet used for the above script:用于上述脚本的工作表:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.