Home > Enterprise >  Auto change cell color based on auto updated cell value in google sheets
Auto change cell color based on auto updated cell value in google sheets

Time:12-18

I need a way/script to auto change the cell background between red and green if the updated cell value which is "=GOOGLEFINANCE("EURUSD")" is higher or lower from the previous update.

Is this even possible to do ?

Any help is highly appreciated.

I tried to modify this code to match my needs but this code doesn't even seem to be working, it keeps spitting: "TypeError: Cannot read properties of undefined (reading 'range')"

function onEdit(e)
{
  var ss = event.range.getSheet();
  if (ss.getName() !== 'Sheet11') return; // Get out quickly
  var changedCell = event.source.getActiveRange();
  var changedCellB8 = changedCell.getB8Notation();
  if (changedCellB8 !== 'B13') return;

  var c = event.value;  // We know we edited cell B3, just get the value
  var background = 'white'; // Assume 1 <= c <= 2
  if (c > 2) {
    background = 'red';
  }
  else if (c < 1) {
    background = 'black'; 
  }
  changedCell.setBackground(background);
  ss.getRange("B8").setBackground(background);
}

CodePudding user response:

You will need conditional formatting. I think that's what it's call. I do most things with code. The cell changes due to formulas do not generate onEdit triggers

conditional format builder

CodePudding user response:

Your code is erroring out because the event parameter is first referred to as e and later as event. Therefore event is undefined, and event.range cannot be read. You can replace e with event to fix the error (but that will still not make the code to do what you want.)

The bigger issue is that you cannot catch formula updates with the onEdit() simple trigger — it only catches manual edits of cell values. Try an "on change" installable trigger instead.

  • Related