Google Script for Conditional Formatting w/ multiple ranges and more than one condition

211 views Asked by At

Sample of the page I am using
I use a conditional format across my sheets but want to change it to Google Script instead.

For every check out date that does not have a Y, highlight yellow.

I managed to modify a script to highlight a cell based on two conditions for A:D but cannot figure out how to extend it to the other ranges in my sheet.

Here is what I have so far that I found :

function onEdit(e) {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A:D");
var values = range.getValues();

//for each row that data is present
for(var i = 0; i < values.length; i++) {
var cell = sheet.getRange(i + 1, 4);

  if(values[i][1] !== "") {


  if(values[i][3] === "") {
    cell.setBackground('yellow');
  } else {
    cell.setBackground(null);
  }

 } else {

  cell.setBackground(null);
}
}  
}    

Links: Google App Script - Conditional Formatting Based on Another Cell

1

There are 1 answers

11
Cooper On

Try this:

function onEdit(e) {
  //e.source.toast('Entry');
  var sh=e.range.getSheet();
  if(sh.getName()=="Sheet2") {
    //e.source.toast('Conditional');
    var vs=sh.getRange(1,1,sh.getLastRow(),4).getValues();
    vs.forEach(function(r,i){
      let rg=sh.getRange(i+1,4);
      if(r[1]=="") {
        if(r[3]=="") {
          //e.source.toast('yellow');
          rg.setBackground('#ffff00');
        }else{
          //e.source.toast('white');
          rg.setBackground('#ffffff');
        }
      }else{
        //e.source.toast('r[1] not null');
        rg.setBackground('#ffffff');
      }
    });
  }
}    

enter image description here

I tested this a little and it seems to work

function onEdit(e) {
  //e.source.toast('Entry');
  var sh=e.range.getSheet();
  if(sh.getName()=="Sheet2") {
    //e.source.toast('Conditional');
    var v=sh.getRange(6,1,sh.getLastRow()-5,sh.getLastColumn()).getValues();
    var r0=sh.getRange(6,4,sh.getLastRow()-5,1);
    var c0=r0.getBackgrounds();
    var r1=sh.getRange(6,9,sh.getLastRow()-5,1);
    var c1=r1.getBackgrounds();
    var r2=sh.getRange(6,14,sh.getLastRow()-5,1);
    var c2=r2.getBackgrounds();
    var r3=sh.getRange(6,19,sh.getLastRow()-5,1);
    var c3=r3.getBackgrounds();
    var r4=sh.getRange(6,24,sh.getLastRow()-5,1);
    var c4=r4.getBackgrounds();
    v.forEach(function(r,i){
      c0[i][0]=(r[1]=="" && r[3]=="")?'#ffff00':'#ffffff';
      c1[i][0]=(r[6]=="" && r[8]=="")?'#ffff00':'#ffffff';
      c2[i][0]=(r[11]=="" && r[13]=="")?'#ffff00':'#ffffff';
      c3[i][0]=(r[16]=="" && r[18]=="")?'#ffff00':'#ffffff';
      c4[i][0]=(r[21]=="" && r[23]=="")?'#ffff00':'#ffffff';
    });
    r0.setBackgrounds(c0);
    r1.setBackgrounds(c1);
    r2.setBackgrounds(c2);
    r3.setBackgrounds(c3);
    r4.setBackgrounds(c4);
  }
}    

Try this for the entire spreadsheet:

function onEdit(e) {
  //e.source.toast('Entry');
  var sh=e.range.getSheet();
  //e.source.toast('Conditional');
  var v=sh.getRange(6,1,sh.getLastRow()-5,sh.getLastColumn()).getValues();
  var r0=sh.getRange(6,4,sh.getLastRow()-5,1);
  var c0=r0.getBackgrounds();
  var r1=sh.getRange(6,9,sh.getLastRow()-5,1);
  var c1=r1.getBackgrounds();
  var r2=sh.getRange(6,14,sh.getLastRow()-5,1);
  var c2=r2.getBackgrounds();
  var r3=sh.getRange(6,19,sh.getLastRow()-5,1);
  var c3=r3.getBackgrounds();
  var r4=sh.getRange(6,24,sh.getLastRow()-5,1);
  var c4=r4.getBackgrounds();
  v.forEach(function(r,i){
    c0[i][0]=(r[1]=="" && r[3]=="")?'#ffff00':'#ffffff';
    c1[i][0]=(r[6]=="" && r[8]=="")?'#ffff00':'#ffffff';
    c2[i][0]=(r[11]=="" && r[13]=="")?'#ffff00':'#ffffff';
    c3[i][0]=(r[16]=="" && r[18]=="")?'#ffff00':'#ffffff';
    c4[i][0]=(r[21]=="" && r[23]=="")?'#ffff00':'#ffffff';
  });
  r0.setBackgrounds(c0);
  r1.setBackgrounds(c1);
  r2.setBackgrounds(c2);
  r3.setBackgrounds(c3);
  r4.setBackgrounds(c4);
}    

Keep in mind if you have multiple people editing the sheet then you are going to be missing some edits because the script will not be able to keep up.