繁体   English   中英

有条件地格式化电子表格单元格

[英]Conditionally Formatting Spreadsheet Cells

我正在尝试创建一个Google Apps脚本,该脚本可让我通过更改单元格的颜色来快速确定是否需要采取进一步的措施。 目前,我正在执行审核过程,在该过程中我会查看产品中5%的错误。 如果发现太多错误,我需要查看产品的100%。

我一直在尝试创建一个脚本,该脚本在达到错误阈值时将更改单元格的颜色,并且需要满足以下条件。 我将使用条件格式,但是选择脚本,这样升级方法就不太明显了,因此将来的扩展可以在需要时操纵文本的格式。

  • 如果a1小于或等于50并且b1小于,则四个颜色单元c1为绿色
  • 如果a1小于或等于50并且b1大于,则四个颜色单元c1红色
  • 如果a1大于或等于50并且b1小于a1的4%,则颜色单元c1为绿色
  • 如果a1大于或等于50并且b1大于a1的4%,则颜色单元c1为红色

任何帮助都将不胜感激!


function onEdit(e) { 
  var ss = e.source.getActiveSheet(); 
  var r = e.source.getActiveRange(); 
  if (r.getRow() != 1 && ss.getName() == "Entry Form") { 
    Error_Count = ss.getRange(r.getRow(),3).getValue(); 
    rowRange = ss.getRange(r.getRow(),1,1,3);

    if (Error_Count < 3.8) {
      rowRange.setBackgroundColor("#FF0000"); }
    else if (Error_Count == 'N/A') {
      rowRange.setBackgroundColor("#ffffff"); }
    else if (Error_Count > 3.9) {
      rowRange.setBackgroundColor("#ffffff");
    }

好吧,您提到了“未来扩展”,但是专门获取“ a1”,“ b1”和“ c1”并不是很可扩展。

我的建议如下:

  1. 使用Sheet类获取“ a1:c100”(具有100行的ac列)。 或者,可以使用Sheet类的 getLastRow() ,然后使用getRange(1, 1, lastRow, 3)以便您在所有数据行中都有ac列(我认为最好的主意)。
  2. 使用getValues函数获取该Range的值。 这将返回一个矩阵。
  3. 由于您的值在矩阵中,因此a1本质上将是values[0][0] b1将是values[0][1] ,而c1将是values[0][2]
  4. 您可以轻松遍历每个“行”值,例如:

     for (var i = 0; i < values.length; i ++) { // do your comparisons for each row. // row[0] is "a_rowNumber", row[1] is "b_rowNumber", etc. var row = values[i]; 
  5. 现在您可以进行比较(纯粹是数学上的比较,我想我会留给您)

  6. 如果比较像:

     if (row[0] > 50 && row[1] < 4) 

    计算结果为true,可以再次使用getRange()函数,如下所示:

     getRange(i + 1, 3) 

    请记住, 是行,第3列是c列:向行添加1,因为您的数组从索引0开始,但是电子表格上的实际行号(由getRange()使用getRange()从1开始。

  7. 一旦有了要在列c中着色的Range(此时是一个单元格),请使用Range.setBackgroundColor() 实际上,可以使用多种方法来设置背景颜色,例如使用RGB,十六进制颜色值或简单的颜色值(如“红色”)。 随便挑!

编辑 :这是如何访问所需数据以及如何使用它的示例:

function onEdit(e) {
  // Get the variable "sheet" so that we can get more ranges and set colors later
  var sheet = e.source.getActiveSheet();
  // Get the edited cell so we can look at the current row
  var cell = e.source.getActiveRange();
  // Get the current row (which is the same row where the cell was edited)
  var editedRow = cell.getRow();

  if (cell.getRow() != 1 && sheet.getName() == "Entry Form") {
    // Get the actual values in that row (the ones we want to use, which are columns A-C)
    var rowValues = sheet.getRange(editedRow, 1, 1, 3).getValues();

    // Your first condition is met: get the current row, column 3 (current row, C), set background color
    if (rowValues[0] <= 50 && rowValues[1] < 4) sheet.getRange(editedRow, 3).setBackgroundColor("green");
    // Your second condition is met: get the current row, column 3 (current row, C), set background color
    else if (rowValues[0] <= 50 && rowValues[1] > 4) sheet.getRange(editedRow, 3).setBackgroundColor("green");
    // Your third condition is met: get the current row, column 3 (current row, C), set background color
    else if (rowValues[0] >= 50 && (rowValues[1] < .04 * rowValues[0])) sheet.getRange(editedRow, 3).setBackgroundColor("green");
    // Your fourth condition is met: get the current row, column 3 (current row, C), set background color
    else if (rowValues[0] >= 50 && (rowValues[1] >= .04 * rowValues[0])) sheet.getRange(editedRow, 3).setBackgroundColor("green");
  }
}

暂无
暂无

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

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