if-statementgoogle-apps-scriptspreadsheet

Condition if-else


I'm trying to send some data from a spreadsheet to a different one. What I need is, if the text of the last row in column B is "APPLE" or "ORANGE" it should go to cell AA7 of the second sheet. In any other case, it should go to cell AC7.

I've created this script but the values always go to cell AC7. Could you help me to see whats wrong with the script?

function comunicacion() {

//Destination sheet
var parte = SpreadsheetApp.openByUrl("url");
var datos = parte.getSheetByName('DATOS');
//Origin sheet
var origen = SpreadsheetApp.getActiveSpreadsheet();
var ws = origen.getSheetByName('REGISTRO');

  var insertarFilaEncima = datos.insertRowsBefore(7,1);

//Datos obtenidos
var ultimaFila = ws.getLastRow();

var tipoComunicacion = ws.getRange(ultimaFila,2).getValue();



if(tipoComunicacion === "APPLE" || "ORANGE") {
datos.getRange("AC7").setValue(tipoComunicacion);
}
else (datos.getRange("AA7").setValue(tipoComunicacion))

}


Solution

  • Handling Multiple Conditions in If Statement

    There is an error in your condition it will always return true due to the syntax use, ORANGE will always be treated as a standalone true value.

    if(tipoComunicacion === "APPLE" || "ORANGE") 
    

    The If statement below will check if it equals Either "APPLE" or "ORANGE"

    if (tipoComunicacion === "APPLE" || tipoComunicacion === "ORANGE")
    

    Sample Code:

    function comunicacion() {
    
      //Destination sheet
      var parte = SpreadsheetApp.openByUrl("Url");
      var datos = parte.getSheetByName('DATOS');
      //Origin sheet
      var origen = SpreadsheetApp.getActiveSpreadsheet();
      var ws = origen.getSheetByName('REGISTRO');
    
      var insertarFilaEncima = datos.insertRowsBefore(7, 1);
    
      //Datos obtenidos
      var ultimaFila = ws.getLastRow();
      var tipoComunicacion = ws.getRange(ultimaFila, 2).getValue();
    
    
      if (tipoComunicacion === "APPLE" || tipoComunicacion === "ORANGE") {
        datos.getRange("AC7").setValue(tipoComunicacion);
      } else {
        datos.getRange("AA7").setValue(tipoComunicacion);
      }
    }
    

    Sample Output:

    REGISTRO Sheet

    DATOS Sheet

    Reference:

    If else Statement