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

232
Views
Cuando creo 2 funciones en java script e inicializo una variable A en la función 1, ¿puedo usar esta variable en la función 2?

Soy un principiante en java script, y tengo un problema aquí y realmente agradecería la ayuda.

Creé una función llamada menú y quiero obtener un valor del usuario en una hoja de Excel y realizar una acción basada en ese valor en otra función. Así que tengo dos preguntas: -

1- Cuando creo 2 funciones en java script e inicializo una variable A en la función 1, ¿puedo usar esta variable en la función 2? No pude probar eso porque estoy usando el script de la aplicación de Google y solo puedo ejecutar una función.

2- (En el código) --> Cómo obtener el valor del uso y usarlo en otra función (Si mi primera pregunta es cierta). ¡El valor que estoy tratando de obtener es el wn, y traté de Logger.log (wn) dos veces y tenían los mismos valores (348-372)!

 function menu() { //get the good week number var date = new Date(); var wn = [(Math.floor(WEEKNUMBER(date)) + 3).toFixed(0)]; do { wn.push((wn[wn.length - 1] - 1).toFixed(0)); } while (wn[wn.length - 1] != 348); var line = "<select style='width:60px;height:40px;' id='select'>"; for (var x in wn) // thats the loop taht is going to show all the weeks for the user line += "<option>" + wn[x] + "</option>"; line += "</select>"; Logger.log(wn); var ui = SpreadsheetApp.getUi(); var html = HtmlService.createHtmlOutputFromFile("Selector") .setWidth(200) .setHeight(150) .setContent( "<div>" + line + "</div><br>. <div><button onclick='reset()'>Confirm</button> </div>. <script>function reset(){var wn = document.getElementById('select').value;document.getElementsByTagName('Body')[0].style.cursor = 'wait';google.script.run.withSuccessHandler(function (){google.script.host.close();}).readWP2(wn);}</script>" ); SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp. .showModalDialog(html, "Please select a week number"); Logger.log(wn); }

Si necesita más explicación por favor hágamelo saber

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

0

Lo que estás preguntando es el alcance variable. No puede declarar una variable en una función y acceder a ella en otra función.

https://www.w3schools.com/js/js_scope.asp

Lo que deberá hacer es pasar la variable a la segunda función como parámetro.

https://www.w3schools.com/js/js_function_parameters.asp

about 4 years ago · Juan Pablo Isaza Report

0

para la primera pregunta, si va a pasar la variable A a la segunda función como argumento dentro de la primera función, no tendrá ningún problema, esto no está relacionado con javascript, esto es lo básico del algoritmo.

para la segunda pregunta, puede hacer que la segunda función devuelva el valor actualizado, por ejemplo:

 A= secondFn(A);
about 4 years ago · Juan Pablo Isaza Report

0

Suponiendo que test1 y test2 se ejecutan secuencialmente. Si no se ejecuta secuencialmente, diga que ejecuta test1 y luego ejecuta test2, entonces necesita usar CacheService si durante la misma sesión o PropertyService si se trata de sesiones diferentes.

 function test1() { var a = "hello"; return a; } function test2(msg) { Logger.log(msg); } function test() { var b = test1(); test2(b); }

Ejecutar prueba.

 7:11:11 AM Notice Execution started 7:11:12 AM Info hello 7:11:12 AM Notice Execution completed
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!