1

我被告知我应该合并我的 if 语句。我不知道该怎么做?另外,这个脚本还有什么问题吗?这是一个谷歌文档脚本。

function onEdit(e) {
  var colorA = "yellow";
  var colorB = "#dddddd";
  var colorC = "#dddddd";

  var sheet = e.source.getActiveSheet();
  var range = e.source.getActiveRange();

  // 3 is column C
  if (range.getColumn() == 3) {
    if (range.getValue() != "") {
      sheet.insertRowAfter(range.getRow());
      var r = range.getRow() + 1;
      sheet.getRange("A" + r + ":H" + r).setBackgroundColor(colorC);
    }
  }

  if (e.source.getActiveRange().getColumn() == 3 ||
      e.source.getActiveRange().getColumn() == 8) {
    var rows = sheet.getMaxRows();
    //two ranges
    //column C
    var rangeC = sheet.getRange("C1:C"+rows);
    var valuesC = rangeC.getValues();
    //column H range
    var rangeH = sheet.getRange("H1:H"+rows);
    var colorH = rangeH.getBackgroundColors();
    var valuesH = rangeH.getValues();

    //iterate over each row in column C and H
    //then change color
        for (var row = 0; row < valuesC.length; row++) {
          //check for columnC and column H
          if (valuesC[row][0] != "" && valuesH[row][0] == "") {
            colorH[row][0] = colorA;
          } else if (valuesH[row][0] != "") {
            colorH[row][0] = colorB;
          }
        }
    sheet.getRange("H1:H" + rows).setBackgroundColors(colorH);
  }
}
​




Here is the other one




ss = SpreadsheetApp.getActiveSpreadsheet();


function onOpen() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [ {name: "New PO", functionName: "NewPO"}];
   ss.addMenu("New PO", menuEntries);
 }


function NewPO() {
  SpreadsheetApp.getActiveSheet().insertRowsBefore(1,6);


  // Adjust this range accordingly, these are the cells that will be
  // copied.  Format is getRange(startRow, startCol, numRows, numCols)
  ss.getSheetByName("PO Form").getRange(1, 1, 6, 8)
      .copyTo(SpreadsheetApp.getActiveSheet().getRange(1, 1, 6, 8));
   }


function onEdit(e) {
  var ss = e.source.getActiveSheet();
  var r = e.source.getActiveRange();
  // 1 is A, 2 is B, ... 8 is H
  if (r.getColumn() == 8 && r.getValue() == "x") {
    r.setValue(Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"));
  }
}
​
4

2 回答 2

2

除了 murray 所指出的之外,还有几个例子可以重复相同的表达:

if (e.source.getActiveRange().getColumn() == 3 ||
  e.source.getActiveRange().getColumn() == 8) {

可能:

var col = e.source.getActiveRange().getColumn();
if(col == 3 || col == 8) {

这在较小程度上适用于:

if (valuesC[row][0] != "" && valuesH[row][0] == "") {
        colorH[row][0] = colorA;
      } else if (valuesH[row][0] != "") {
        colorH[row][0] = colorB;
      }

这可能是(例如):

var hRow = colorH[row];
if (valuesC[row][0] != "" && valuesH[row][0] == "") {
        hRow[0] = colorA;
      } else if (valuesH[row][0] != "") {
        hRow[0] = colorB;
      }
于 2010-12-09T07:19:52.017 回答
1

我唯一能看到的:

// 3 是 C 列 if (range.getColumn() == 3) { if (range.getValue() != "") {

// 3 是 C 列 if (range.getColumn() == 3 && range.getValue() != "") {

于 2010-12-09T07:15:39.130 回答