根据值更改单元格背景颜色onEdit [英] Change cell background color onEdit based on value

查看:132
本文介绍了根据值更改单元格背景颜色onEdit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据 onEdit()函数中的单元格内容更改单元格背景颜色?



我有很多版本的代码,我为此进行了测试 - 一些工作几乎正确,有些不工作。
但是我还没有按照我需要的方式让它工作。



请原谅这样写的方式缺乏优雅,但我实际上需要尽可能保持代码的简洁性,因为会有很多单元格更改,许多条件和许多不同的单元格数量,这些单元格将根据工作表上发生的更改而进行更改。



好的,所以这里...

 函数onEdit(event)
{
var ss = SpreadsheetApp.getActiveSpreadsheet()。getSheetByName(Sheet2);
var changedCell = event.source.getActiveRange()。getA1Notation();

if(changedCell =='B3'){
var c = ss.getRange(B3)。getValue();
if(c <2); {
ss.getRange(B3)。setBackgroundColor('#ff0000');
ss.getRange(B12)。setBackgroundColor('#ff0000');
}
if(c> 1); {
ss.getRange(B3)。setBackgroundColor('#000000');
ss.getRange(B12)。setBackgroundColor('#000000');




解决方案



1.该方法的名称是 setBackground ,而不是 setBackgroundColor



2.单元格B3是如何格式化的。只有格式化为整数时,该比较才有效。在大多数情况下,Google Spreadsheet会根据数据类型自动格式化单元格,但如果我正在编写代码,那么我会仔细检查。因此,请使用类似于

  var c = parseInt(ss.getRange(B3)。getValue()); 



<3>如果不需要分号,如果条件。这将立即终止if条件。所以使用

  if(c <2){

  if(c> 1){

4.最后,我不知道数据是如何进入B3的,但如果您在B3中有1.5个数据, if if 条件成为true并且您的背景颜色被覆盖。所以,我建议你使用if..elseif



为了提高可读性,我会使用 setBackground('red') setBackground('white')为常见的颜色。

How can I change the cell background color based on the cell's content in an onEdit() function?

I've had many versions of code that I tested for this - some working almost right, some not working at all. But I have yet to get this to work the way I need it to.

Please forgive the lack of elegance in the way that this is written, but I actually need to keep the code as straightforward as possible since there will be many cell changes, many conditionals, and many differing numbers of cells that will be changed depending on what gets changed on the worksheet.

Ok, so here goes...

function onEdit(event)
{
 var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");   
 var changedCell= event.source.getActiveRange().getA1Notation();

 if (changedCell == 'B3') {
 var c = ss.getRange("B3").getValue();
  if (c < 2); {
  ss.getRange("B3").setBackgroundColor('#ff0000');  
  ss.getRange("B12").setBackgroundColor('#ff0000'); 
  }
  if (c > 1); {
  ss.getRange("B3").setBackgroundColor('#000000');  
  ss.getRange("B12").setBackgroundColor('#000000'); 
  }
 }
}

解决方案

A few things to note

1.The name of the method is setBackground and not setBackgroundColor

2.How is the cell B3 formatted. The comparison works only if it is formatted as an integer. In most cases, Google Spreadsheet automatically formats the cells based on the data type, but if I'm writing code, I'd double check. So use something like

 var c = parseInt(ss.getRange("B3").getValue()) ;

3.The semicolon is not needed after the if condition. That will terminate the if condition immediately. So use

if (c < 2) {

and

if (c > 1) {

4.Finally, I don't know how data comes into B3, but if you have 1.5 in B3, then both the if conditions become true and your background color is overwritten. So, I suggest that you use a if..elseif

For better readability, I'd use setBackground('red') and setBackground('white') for the common colours.

这篇关于根据值更改单元格背景颜色onEdit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆