vendredi 12 mars 2021

Google Apps Script multiple IF statements in an onEdit function

Background

I'm trying to write a script that will check two different cells to see if the checkbox has been checked. So, it would first check one cell (AL4) to see if the value is true, and then a different cell (Q4) to see if the value is true. If the value is true, then it will run a different function.

Both of the cells I am checking are in the same sheet, "Dashboard", so it doesn't have to find different sheet names. I've been different ways to get it to check the different cells by using .getcolumn and .getrow commands but I've had some trouble with that too. I've also run into some problems where it will ignore the if statement and run the function on every edit and not just when the two cells are checked.

How can I get an if and else if statement to work in the same onEdit function?

Current Script

function onEdit(e) {
  const sheetName = "Dashboard"
  var range = e.range;
  
  if (range.getSheet().getSheetName() != sheetName || range.getA1Notation() != "AL4" || !range.isChecked()) {
    return;
    
    RefreshCheckmark2();
  } else if (range.getSheet().getSheetName() != sheetName || range.getA1Notation() != "Q4" || !range.isChecked()) {
    return;
    
    InverseYieldRefresh();
  }
}

Also, the above code is completely functional if you remove the else if statement and the brackets after the first If statement.

Ideally I would like to be able to add more else if statements so that it can check more cells because I like to have checkboxes run commands and if my understanding is correct, you can't have more than one onEdit function in a single spreadsheet.

Thank you for any insight!

Aucun commentaire:

Enregistrer un commentaire