1

我已经尝试过:当单元格更改文本但无法使其正常工作时更改行颜色的脚本。行的颜色不会更改为#000000。这是我到目前为止所拥有的:

function onEdit(event)
{
  var ss = event.source.getActiveSheet();
  var r = event.source.getActiveRange();

  var currentValue = r.getValue();

  if(currentValue == "dags dato")
  {
    var dd = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd");
    r.setValue(dd);
  }
  else if(currentValue == "dialog")
  {
    setRowColor("yellow");
  }
  else if(currentValue == "besvaret")
  {
    setRowColor("yellow");
  }
  else if(currentValue == "afvist")
  {
    setRowColor("red");
  }
}

function setRowColor(color)
{
  var range = SpreadsheetApp.getActiveSheet().getDataRange();
  var statusColumnOffset = getStatusColumnOffset();

  for (var i = range.getRow(); i < range.getLastRow(); i++) {
    rowRange = range.offset(i, 0, 1);
    status = rowRange.offset(0, statusColumnOffset).getValue();

    rowRange.setBackgroundColor("#000000");

}


//Returns the offset value of the column titled "Status"
//(eg, if the 7th column is labeled "Status", this function returns 6)
function getStatusColumnOffset() {

  lastColumn = SpreadsheetApp.getActiveSheet().getLastColumn();
  var range = SpreadsheetApp.getActiveSheet().getRange(1,1,1,lastColumn);

  for (var i = 0; i < range.getLastColumn(); i++) {
    if (range.offset(0, i, 1, 1).getValue() == "Status") {
      return i;
    } 
  }
}
4

2 回答 2

2

我为自己写了更快更干净的方法,我想分享它。

function onEdit(e) {
    if (e) { 
        var ss = e.source.getActiveSheet();
        var r = e.source.getActiveRange(); 

        // If you want to be specific
        // do not work in first row
        // do not work in other sheets except "MySheet"
        if (r.getRow() != 1 && ss.getName() == "MySheet") {

            // E.g. status column is 2nd (B)
            status = ss.getRange(r.getRow(), 2).getValue();

            // Specify the range with which You want to highlight
            // with some reading of API you can easily modify the range selection properties
            // (e.g. to automatically select all columns)
            rowRange = ss.getRange(r.getRow(),1,1,19);

            // This changes font color
            if (status == 'YES') {
                rowRange.setFontColor("#999999");
            } else if (status == 'N/A') {
                rowRange.setFontColor("#999999");
            // DEFAULT
            } else if (status == '') { 
                rowRange.setFontColor("#000000");
            }   
        }
    }
}
于 2012-12-05T20:28:41.537 回答
-1

您可以尝试使用 Logger 类检查您的代码是否有任何错误或问题,如下所示:

try {
    //your code
}
catch(e) {
    Logger.log(e);
}

然后,您可以从脚本编辑器转到查看 -> 日志,查看每行代码是否按预期执行。此外,执行记录可能有助于查看代码是否在某一特定代码行中断。您可以查看有关每种故障排除方法如何工作的更多详细信息

于 2012-04-30T14:58:27.837 回答