【问题标题】:Change color on active row based on active cell content根据活动单元格内容更改活动行的颜色
【发布时间】:2014-06-21 21:03:38
【问题描述】:
刚刚开始使用 google 电子表格脚本。
我正在尝试根据活动单元格中的内容更改活动行的背景颜色。
我不知道为什么这不起作用,希望你能帮忙:)
function setStatusColor() {
var ActiveSheet = SpreadsheetApp.getActiveSheet();
var ActiveRow = ActiveSheet.getActiveRange().getRow();
var ActiveCell = ActiveSheet.getActiveCell();
if (ActiveCell == 'Plan') {
ActiveRow.setBackgroundColor("#FFFFFF");
} else if (ActiveCell == 'Offer') {
ActiveRow.setBackgroundColor("#FFFF00");
} else if (ActiveCell == 'Confirmed') {
ActiveRow.setBackgroundColor("#00FF00");
} else if (ActiveCell == 'Canceled') {
ActiveRow.setBackgroundColor("#FF0000");
} else {
ActiveRow.setBackgroundColor("#FFFFFF");
}
}
【问题讨论】:
标签:
google-apps-script
google-sheets
【解决方案1】:
我是这样解决的:
function onEdit(e) {
if (e) {
var ss = e.source.getActiveSheet();
var r = e.source.getActiveRange();
if (r.getRow() != 1 && ss.getName() == "Sheet1") {
// Status in column 6 (F)
status = ss.getRange(r.getRow(), 6).getValue();
// Range to put background color to (rowStart, numberOfRows, columnStart, numberOfColumns)
rowRange = ss.getRange(r.getRow(),1,1,6);
if (status == 'Plan') {
rowRange.setBackgroundColor("white");
} else if (status == 'Offer') {
rowRange.setBackgroundColor("yellow");
} else if (status == 'Confirmed') {
rowRange.setBackgroundColor("green");
} else if (status == 'Canceled') {
rowRange.setBackgroundColor("red");
} else if (status == '') {
rowRange.setBackgroundColor("white");
}
}
}
}
【解决方案2】:
这应该可以工作:
function setStatusColor() {
var ActiveSheet = SpreadsheetApp.getActiveSheet();
var ActiveRow = ActiveSheet.getActiveRange();
var ActiveCell = ActiveSheet.getActiveCell().getValue();
if (ActiveCell == 'Plan') {
ActiveRow.setBackgroundColor("#FFFFFF");
} else if (ActiveCell == 'Offer') {
ActiveRow.setBackgroundColor("#FFFF00");
} else if (ActiveCell == 'Confirmed') {
ActiveRow.setBackgroundColor("#00FF00");
} else if (ActiveCell == 'Canceled') {
ActiveRow.setBackgroundColor("#FF0000");
} else {
ActiveRow.setBackgroundColor("#FFFFFF");
}
}
【解决方案3】:
ActiveCell 实际上是一个范围。试试这个:
var ActiveCell = ActiveSheet.getActiveCell().getValue();