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

230
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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