根据活动单元格内容更改活动行上的颜色

时间:2014-05-05 14:07:27

标签: google-apps-script google-sheets

刚刚开始使用谷歌电子表格脚本。

我尝试根据活动单元格中的内容更改活动行的背景颜色。

我不确定为什么这不起作用,希望你能提供帮助:)

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");
  }
}

3 个答案:

答案 0 :(得分:0)

ActiveCell实际上是一个范围。试试这个:

var ActiveCell = ActiveSheet.getActiveCell().getValue();

答案 1 :(得分:0)

这应该有效:

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");
  }
}

答案 2 :(得分:0)

我解决了这个问题:

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");
            }   
        }
    }
}