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

147
Visualizações
return 2 values javascript

I'm trying to return two values in GAS like this:

Html

<script>
  function phoneSearch() {
    var phone = document.getElementById("phone").value;
    google.script.run.withSuccessHandler(onSuccess).phoneSearch(phone);
  }
  function onSuccess(name, email) {
    document.getElementById('nameDiv').innerHTML = "<div>" + name + "</div>";
    document.getElementById('emailDiv').innerHTML = "<div>" + email+ "</div>";
  } 
</script>

Server JS

function phoneSearch(phone){
  try {
    var ss = SpreadsheetApp.getActive();
    var sheet = ss.getSheetByName("sheetNameHere");
    var data = sheet.getDataRange().getValues();
    for (var i = 1; i < data.length; i++) {
      if (data[i][4] == phone) {
        var name = inputData[i][3];
        var email = inputData[i][5];
        return name;
        return email;
      }
    }
  } catch(e) {
    alert(e)
  }
}

Only the first value (name) returns. Thanks in advance!

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

0

You can't use more one 'return' at the same level, you should return object containing your two values.

return {
   phone: phone,
   email: email
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can only return 1 value from your function at a time, but you can easily wrap that in an object:

<script>
  function phoneSearch() {
    var phone = document.getElementById("phone").value;
    google.script.run.withSuccessHandler(onSuccess).phoneSearch(phone);
  }
  function onSuccess(result) {
  //                  ^ phoneSearch now returns -one- object, containing `name` and `email`.
    document.getElementById('nameDiv').innerHTML = "<div>" + result.name + "</div>";
    document.getElementById('emailDiv').innerHTML = "<div>" + result.email+ "</div>";
  } 
</script>

JS

function phoneSearch(phone){
  try {
    var ss = SpreadsheetApp.getActive();
    var sheet = ss.getSheetByName("sheetNameHere");
    var data = sheet.getDataRange().getValues();
    for (var i = 1; i < data.length; i++) {
      if (data[i][4] == phone) {
        var name = inputData[i][3];
        var email = inputData[i][5];
        // Return both values at once. 
        return { name, email };
        // (This is shorthand for: `return { name: name, email: email };`)
      }
    }
  } catch(e) {
    alert(e)
  }
}
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