Question

Just getting started with google spreadsheets scripts.

I'm trying to change the background color of the active row based on the content in the active cell.

I'm not sure why this doesn't work, hope you can help :)

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");
  }
}
Était-ce utile?

La solution 3

I solved it like this:

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

Autres conseils

ActiveCell is a range actualy. Try with this:

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

This ought to work:

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");
  }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top