Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

231
Vistas
When i create 2 functions in java script and initialize a variable A in function 1, can I use this variable in function2?

I am a beginner in java script, and I have got a problem here and would really appreciate the help.

I created a function called menu and I want to get a value from the user in an excel sheet and do an action based on that value in another function. So I have two questions:-

1- When i create 2 functions in java script and initialize a variable A in function 1, can I use this variable in function2? I couldn't test that because I'm using google app script and I can only run one function.

2- (In the code) --> How to get the value from the use and use it in another function (If my first question is true). The value I'm trying to get is the wn, and i tried to Logger.log(wn) twice and they had the same values(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);
}

If you need more explanation please let me know

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

What you're asking about is variable scope. You cannot declare a variable in a function and access it in another function.

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

What you will need to do is pass the variable to the second function as a parameter.

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

about 4 years ago · Juan Pablo Isaza Denunciar

0

for the first question, if you are going to pass the A variable to the second function as an argument inside the first function you won't face any problem, this is not related to javascript, this is algorithm basics.

for the second question, you can just make the second function return the updated value, for example:

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

0

Assuming test1 and test2 are run sequentially. If not sequentially say you run test1 and later run test2 then you need to use CacheService if during the same session or PropertyService if different sessions.

function test1() {
  var a = "hello";
  return a;
}

function test2(msg) {
  Logger.log(msg);
}

function test() {
  var b = test1();
  test2(b);
}

Run test.

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda