Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

Google Apps Script setValue() AND setBackgroundRGB() for the same cell

I'm trying to use the methods setValue and setBackgroundRGB together for the same cell but it doesn't work. Actually, only setValue works. I tried the 3 different ways of the following if-else condition but always I see only the number without any change at background colour. If I delete the number, only then I see the colour. Is it possible to have both for the same cell?

(Also, is there a way to work in specific columns without using the active cell?)

function onEdit(e) {
  var active_cell = e.range;
  var active_column = active_cell.getColumn();

      if(active_sheet.getName().startsWith("202")){
        var in_row= 5;
        var exp_row= 6;
        var diff_row = 7;
    
        var income = active_sheet.getRange(in_row,active_column).getValue();
        var expenses = active_sheet.getRange(exp_row,active_column).getValue();
        var diff = income - expenses;
    
        if (active_column >= 2){
          if(income > expenses) {
            active_sheet.getRange(diff_row,active_column).setBackgroundRGB(255,0,0).setValue(diff);
          } else if (income == expenses) {
            active_sheet.getRange(diff_row,active_column).setValue(diff).setBackgroundRGB(0,255,0);
          } else {
            active_sheet.getRange(diff_row,active_column).setValue(diff);
            active_sheet.getRange(diff_row,active_column).set.setBackgroundRGB(255,0,0);
          }
        }
     }

}
question from:https://stackoverflow.com/questions/65908685/google-apps-script-setvalue-and-setbackgroundrgb-for-the-same-cell

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

active_sheet is not defined in your code, Range.setBackgroundRGB() and Range.setValue() are working as expected in either of your combinations.

Updated Code:

function onEdit(e) {
  var active_sheet = e.source.getActiveSheet();
  var active_cell = e.range;
  var active_column = active_cell.getColumn();

      if(active_sheet.getName().startsWith("202")){
        var in_row= 5;
        var exp_row= 6;
        var diff_row = 7;
    
        var income = active_sheet.getRange(in_row,active_column).getValue();
        var expenses = active_sheet.getRange(exp_row,active_column).getValue();
        var diff = income - expenses;
    
        if (active_column >= 2){
          if(income > expenses) {
            active_sheet.getRange(diff_row,active_column).setBackgroundRGB(255,0,0).setValue(diff);
          } else if (income == expenses) {
            active_sheet.getRange(diff_row,active_column).setValue(diff).setBackgroundRGB(0,255,0);
          } else {
            active_sheet.getRange(diff_row,active_column).setValue(diff);
            active_sheet.getRange(diff_row,active_column).setBackgroundRGB(255,0,0);
          }
        }
     }

}

Output:

enter image description here


Regarding your other question: Is there a way to work in specific columns without using the active cell?

  • Yes, you can select a specific column as your range without using the active cell's column index. You just need to provide a hard-coded column index when selecting a range using Sheet.getRange(row, column).

Example:

var income = active_sheet.getRange(in_row,4).getValue();
  • Here, I will access the cell in column D (which is in index 4).

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...