Google Sheets - Google Apps Script - ElseIf Statement

I am trying to create a function for Google Sheets but am getting confused with the comparison operators and elseif format.

The function points to a specific cell in my sheet and moves a range between worksheets based on the Status value: Screenshot

I want to do the following:

If Status value = "Pending" - move range to worksheet ORDERS | Active

If Status value = "In Hand" - move range to worksheet ORDERS | Complete

If Status value = "For Sale" - move range to worksheet SALES | Active

If Status value = "Sold" - move range to worksheet SALES | Complete

function onEdit(e) {
  const src = e.source.getActiveSheet();
  const r = e.range;
  if (r.columnStart != 2 || r.rowStart == 1 || e.value == ("In Hand"));
  const dest = e.source.getSheetByName('ORDERS | Complete');
  src.getRange(r.rowStart,1,1,12).moveTo(dest.getRange(dest.getLastRow()+1,1,1,12));
  src.deleteRow(r.rowStart);
}

Thanks

EDIT 1

I have some questions regarding the answer below (that works).

  1. It is slowing down my sheet quite a lot - does this definitely only run if the specified range is edited - or does it run after EVERY edit anywhere on the sheet (and then checks if the specified range is edited)? Ideally I'd like it to run only on the 4 worksheets listed: ORDERS | Active, ORDERS | Complete, SALES | Active, SALES | Complete

  2. Using the get column and get row method - will this ensure the function runs on every row in column 2 that is edited (which is what I want) - or just column 2 row 1 on the active sheet?


Solution 1:

There seem to be a few issues with your (incomplete) code that could be to blame:

  1. The || operator in your if statement is an OR operator, which I don't believe you want. You likely want to be using the && (AND) operator, as this means the body of the if statement will only execute when the starting row/column are correct AND the cell value is correct.
  2. It seems like you probably want to use a == operator for r.getColumn() rather than !=, as you want to be checking the value of the cell only if it's the status column (column B, aka column 2).
  3. Your if statement doesn't appear to actually be doing anything since it ends with a semicolon. In JavaScript (and Google Apps Script), a semicolon ends the statement, so your if statement is being immediately terminated without the chance to execute any logic. We can fix this by moving the other statements into a group of curly braces:
function onEdit(e) {
  const src = e.source.getActiveSheet();
  const r = e.range;
  if (r.columnStart != 2 && r.rowStart == 1 && e.value == ("In Hand")) {
    const dest = e.source.getSheetByName('ORDERS | Complete');
    src.getRange(r.rowStart,1,1,12).moveTo(dest.getRange(dest.getLastRow()+1,1,1,12));
    src.deleteRow(r.rowStart);
  }
}
  1. We could use several if-else statements here to implement logic for each of the possible status values, but that would require a lot of unnecessary code repetition. A switch-case seems like the better way to approach this:
function onEdit(e) {
  const src = e.source.getActiveSheet();
  const r = e.range;
  if (r.getColumn() == 2 && r.getRow() == 1 && r.getWidth() == 1 && r.getHeight() == 1) {
    var sheetName;
    switch (e.value) {
      case "Pending":
        sheetName = "ORDERS | Active";
        break;
      case "In Hand":
        sheetName = "ORDERS | Complete";
        break;
      case "For Sale":
        sheetName = "SALES | Active";
        break;
      case "Sold":
        sheetName = "SALES | Complete";
        break;
    }
    if (sheetName) {
      const dest = e.source.getSheetByName(sheetName);
      src.getRange(r.getRow(),1,1,12).moveTo(dest.getRange(dest.getLastRow()+1,1,1,12));
      src.deleteRow(r.getRow());
    }
  }
}

The above switch-case compares the cell value to the valid statuses and sets the appropriate sheet name if the status is valid. Then, we can manipulate that sheet in any way we want.

As a side note: I added two additional conditions to the if statement to check if the range size is 1x1, as e.value won't be available if the range is bigger than a cell.

Edit: The above code blocks use r.getColumn() and r.getRow() in place of r.columnStart and r.rowStart, respectively, since those methods are documented while the properties are not, although it is a matter of preference.