简体   繁体   English

如何使用大于和小于事件执行 onEdit?

[英]How do I execute onEdit with greater than and less than events?

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我需要一个实际工作的 gs 代码,但我似乎找不到正确的 function,所以我抓取了一个可解释的版本,它像 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 :)提前非常感谢您:我真的需要帮助,任何人都将不胜感激 :)

Use an onEdit(e) simple trigger and the event object , like this:使用onEdit(e)简单触发器事件 object ,如下所示:

/**
* 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM