Question

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"));
  }
}
​
Was it helpful?

Solution

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

OTHER TIPS

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() != "") {

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top