• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

137
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

almost 3 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

almost 3 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);
almost 3 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
almost 3 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda