1

I was told I should consolidate my if statements. I'm not sure how to do this? Also, is there anything else wrong in this script? It is for a google doc script.

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 Answers 2

2

Besides what murray noted, there are several instances where you repeat the same expression:

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

could be:

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

This applies to a lesser extent to:

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

which could be (for instance):

var hRow = colorH[row];
if (valuesC[row][0] != "" && valuesH[row][0] == "") {
        hRow[0] = colorA;
      } else if (valuesH[row][0] != "") {
        hRow[0] = colorB;
      }
Sign up to request clarification or add additional context in comments.

1 Comment

+1 Local variables are quicker to access than references to external object properties. You could also change "for (var row = 0; row < valuesC.length; row++)" to "for (var row = 0, len = valuesC.length; row < len; row++)" so the loop doesn't have to check the 'valuesC.length' reference on every iteration of the loop. Keep in mind that conditional code in a for loop is executed every iteration. If you step through a debugger for a compiled language (ex. C#) you can watch these jumps happen visually. On desktop apps the performance hit is less significant but in JS every little bit helps.
1

only thing i can see:

// 3 is column C if (range.getColumn() == 3) { if (range.getValue() != "") {

// 3 is column C if (range.getColumn() == 3 && range.getValue() != "") {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.