Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
Compare multiple strings in one conditional if in Google Sheets

I would like to do the following with Google Apps Script:

  1. Compare more than two strings corresponding to different versions of the same car model
  2. Match the correct model
  3. Use a Google Doc template with the model's info

Here you can find part of my code:

  const rows = sheet.getDataRange().getValues();
  var googleDocTemplate = DriveApp.getFileById('doc ID')

rows.forEach(function(row,index) {
    if (index === 0) return;
    if (row[0]) return;
    if (row[3]==='Toyota') return;
    if (row[3]==='Ford') return;
    if (row[3]==='Dodge') return;
    if (row[3]==='Changan') return;
    if (row[15]==='' && row[22]==='') return;
    
//Grand i10 M/T GL, GLS or A/T GLS

if ((row[4].equals('Grand i10 M/T GL') || row[4].equals('Grand i10 M/T GLS') || row[4].equals('Grand i10 A/T GLS')) && row[9]===0 && row[6]==='Electric Blue'){
    googleDocTemplate = DriveApp.getFileById('doc ID'); 
  } 

else if ((row[4].includes('Grand i10 M/T GL') || row[4].includes('Grand i10 M/T GLS') || row[4].includes('Grand i10 A/T GLS')) && row[9]===0 && row[6]==='Black'){
    googleDocTemplate = DriveApp.getFileById('doc ID'); 
  }  

else {
  SpreadsheetApp.getUi().alert("Hyundai model is wrong")
  return
}

Basically the code search through rows in the Google Sheets like this

and then compares the string contained in the cell with the reference string (e.g 'Grand i10 M/T GL'), but in this particular case, the conditional jumps straight to the else showing the alert "Hyundai model is wrong".

Any help would be appreciated. Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try this. I didn't know how you handle Hyundai

function test() {
  try {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName("Sheet4");  // substitute your sheet here
    var rows = sheet.getDataRange().getValues();
    var i = 0;
    // i=1 skip first row?
    for( i=1; i<rows.length; i++ ) {
      // note index 3 is column D
      if( ( ( rows[i][3] === "Grand i10 M/T GL" )
         || ( rows[i][3] === "Grand i10 M/T GLS" )
         || ( rows[i][3] === "Grand i10 A/T GLS" ) )
       && ( rows[i][8] === 0 )
       && ( ( row[i][5] === "Electric Blue" )
       ||   ( row[i][5] === "Black" ) ) ) {
        googleDocTemplate = DriveApp.getFileById('doc ID');
        break;  // asuming there is only one row with these attributes
      }
      else if( rows[i][2] === "Hyundai" ) {
        SpreadsheetApp.getUi().alert("Hyundai model is wrong");
        return;
      }
    }
    Logger.log("row = "+(i+1));
  }
  catch(err) {
    Logger.log(err);
  }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda