Home > Enterprise >  If and Else is inverted
If and Else is inverted

Time:05-31

I have this code here, but it's inverted somehow...

function OnEdit(e) {
  const range = e.range;
  const sheet = range.getSheet();
  if (sheet.getSheetName() != "Formulário" || range.getA1Notation() != "D7" || range.getValue() != "Sucesso do Cliente"){

    var url = "https://docs.google.com/spreadsheets/d/1BbuJfPPOSdbvHZ5b8xVTMndeydNfslDhPLm9ftL1pLU/edit?usp=sharing";
    const html = `<script>window.open('${url}', '_blank');google.script.host.close();</script>`;
    SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutput(html), "Carregando...");
    
} else if (sheet.getSheetName() != "Formulário" || range.getA1Notation() != "D7" || range.getValue() != "Faturamento"){

      var url = "https://docs.google.com/spreadsheets/d/1o1lKauuBXFTXb2jEEF5t9wAtaYLBPFYu9Y3T_XnO5rk/edit#gid=0";
      const html = `<script>window.open('${url}', '_blank');google.script.host.close();</script>`;
      SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutput(html), "Carregando...");
        
}

}

When cell D7 = "Sucesso do Cliente", I am returned the url that is inside the else and not the If

And when cell D7 = "Faturamento", I am returned the URL that is inside the If (which is the URL of "Sucesso do Cliente"), instead of the url that is inside the else

CodePudding user response:

Try it this way:

function OnEdit(e) {
  const sheet = e.range.getSheet();
  if (sheet.getSheetName() == "Formulário" && e.range.getA1Notation() == "D7" || e.range.getValue() == "Sucesso do Cliente") {

    var url = "https://docs.google.com/spreadsheets/d/1BbuJfPPOSdbvHZ5b8xVTMndeydNfslDhPLm9ftL1pLU/edit?usp=sharing";
    const html = `<script>window.open('${url}', '_blank');google.script.host.close();</script>`;
    SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutput(html), "Carregando...");

  } else if (sheet.getSheetName() == "Formulário" || e.range.getA1Notation() == "D7" || e.range.getValue() == "Faturamento") {

    var url = "https://docs.google.com/spreadsheets/d/1o1lKauuBXFTXb2jEEF5t9wAtaYLBPFYu9Y3T_XnO5rk/edit#gid=0";
    const html = `<script>window.open('${url}', '_blank');google.script.host.close();</script>`;
    SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutput(html), "Carregando...");

  }

}

You really have to read the code to figure out what it is saying

  • Related