0

I am trying to create a Google Apps Script that will allow me to quickly determine if further action is needed by changing color of a cell. Currently I am performing an auditing process where I look at 5% of the product for errors. If too many errors are found, I need to look at 100% of the product.

I have been trying to create a script that upon meeting the error threshold will change the color of a cell and the following criteria is what needs to be met. I would use conditional formatting but opt for a script so that the escalation method is less apparent and so future expansion can manipulate formatting of text if needed.

  • If a1 is less than or equal to 50 and b1 is less then four color cell c1 green
  • If a1 is less than or equal to 50 and b1 is greater then four color cell c1 red
  • If a1 is more than or equal to 50 and b1 is less than 4% of a1 then color cell c1 green
  • If a1 is more than or equal to 50 and b1 is greater than 4% of a1 then color cell c1 red

Any and all help is greatly appreciated!


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");
    }
4

1 回答 1

0

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

我的建议如下:

  1. 使用Sheet 类获取“a1:c100”(100 行的列 ac)。或者也许使用getLastRow()Sheet,然后使用getRange(1, 1, lastRow, 3)这样你的列 ac 包含所有包含数据的行(我认为最好的主意)。
  2. getValues使用该函数获取该 Range 的值。这将返回一个矩阵。
  3. 由于您的值在矩阵中,因此a1本质上是values[0][0]. b1values[0][1]c1values[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)
    

    评估为真,您可以getRange()再次使用该函数,如:

    getRange(i + 1, 3)
    

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

  7. 一旦有了要在 c 列中着色的范围(此时是一个单元格),请使用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");
  }
}
于 2013-11-19T22:40:54.827 回答