dimanche 29 septembre 2019

Googlescript 'if' question - do only one of two tasks

I have 'else if' statements and I need it to be called only if the previous 'if' does not execute. First 'if' (check status) work perfect, second work to, but the 'else if' is done every time because there are different types in the table (A, B, C e.t.c) Here is my code:

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SheetName');
  var data = sheet.getRange(2,1,sheet.getLastRow(),2).getValues();
  var nextRow = SpreadsheetApp.getActiveSheet().getDataRange().getLastRow()+1;

  for (var i=0; i<data.length; i++){
    var row = data[i];
    var type = row[0];
    var status = row[1];

//check it always     
    if (status != 'close'){

//check first and if it's true don't do 'else if'      
       if (type == 'A') {
         sheet.getRange(i+2,2).setValue('close')
       }

//this should only be called if the previous 'if' is not true      
      else if (type != 'A' && type != ''){
        var values = [['A','open']];
        sheet.getRange("A"+nextRow+":B"+nextRow).setValues(values); 
      }
    }
  }
}

Aucun commentaire:

Enregistrer un commentaire