Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
devuelve 2 valores javascript

Estoy tratando de devolver dos valores en GAS como este:

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>

Servidor 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) } }

Solo se devuelve el primer valor (nombre). ¡Gracias por adelantado!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

No puede usar más de un 'retorno' en el mismo nivel, debe devolver el objeto que contiene sus dos valores.

 return { phone: phone, email: email }
about 4 years ago · Juan Pablo Isaza Report

0

Solo puede devolver 1 valor de su función a la vez, pero puede envolverlo fácilmente en un objeto:

 <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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!