Javascript整合if语句

时间:2010-12-09 07:12:32

标签: javascript google-apps-script google-sheets

我被告知我应该巩固我的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"));
  }
}
​

2 个答案:

答案 0 :(得分:2)

除了默里所指出的,有几个例子你重复相同的表达:

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;
      }

答案 1 :(得分:1)

我唯一能看到的是:

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

// 3是C列   if(range.getColumn()== 3&amp;&amp; range.getValue()!=“”){