我有一个 Google 电子表格,其中保存了错误列表,每当我修复错误时,我都会将状态从“未开始”更改为“完成”。我想为 Google 文档电子表格编写一个脚本,以便每当我将状态更改为“完成”时,整行就会以某种颜色突出显示。
我已经知道 Google 电子表格已经具有“更改文本颜色”功能,但该功能仅更改单元格的颜色,不会更改整行的颜色。
最佳答案
//Sets the row color depending on the value in the "Status" column.
function setRowColors() {
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 == 'Completed') {
rowRange.setBackgroundColor("#99CC99");
} else if (status == 'In Progress') {
rowRange.setBackgroundColor("#FFDD88");
} else if (status == 'Not Started') {
rowRange.setBackgroundColor("#CC6666");
}
}
}
//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;
}
}
}
关于google-apps-script - 当单元格更改文本时更改行颜色的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3703676/