0

I need an actual working gs code for this but I cant seem to find the correct function for this so I scraped an explainable version of this, it works like an onEdit function

function(value, modifier) {
 if (value < modifier) = setBackground("red");
}



function(value, modifier) {
 if (value > modifier) = setBackground("blue");
}

what I want: -change cell color and cell highlight color to red if new input value is greater than previous one -change cell color and cell highlight color to blue if new input value is less than previous one

Thank you very much in advance! I really need help and any would be greatly appreciated :)

iXanderM
  • 47
  • 3
  • 7

1 Answers1

1

Use an onEdit(e) simple trigger and the event object, like this:

/**
* Simple trigger that runs each time the user manually edits the spreadsheet.
*
* @param {Object} e The onEdit() event object.
*/
function onEdit(e) {
  if (!e) {
    throw new Error(
      'Please do not run the onEdit(e) function in the script editor window. '
      + 'It runs automatically when you hand edit the spreadsheet. '
      + 'See https://stackoverflow.com/a/63851123/13045193.'
    );
  }
  highLightNumberChanges_(e);
}


/**
* When a number is entered, and the value that was previously in the
* cell was also a number, set cell fill color to red if the new value
* is greater than the previous value, and to blue if it is less.
*
* @param {Object} e The onEdit() event object.
*/
function highLightNumberChanges_(e) {
  if (isNaN(e.value) || isNaN(e.oldValue)) {
    return;
  }
  const color = Number(e.value) > Number(e.oldValue) ? 'red' : 'blue';
  e.range.setBackgroundColor(color);
}
doubleunary
  • 13,842
  • 3
  • 18
  • 51