title: Global variables in Google Script (spreadsheet)
body: <p>I've been trying to get some functions in a Google apps script (inside a spreadsheet) to modify a global variable, but I can't seem to figure it out. </p>
  
  <p>Basically I want to declare a variable (in this case "globalTestVar"), and every time one of the two functions (globalVarTestFunctionOne and two) launches this variable should increment by one. </p>
  
  <p>The problem is that the variable is being declared again every time a button is pressed even though the if(typeof(globalTestVar) == 'undefined')-statement should take care of that. </p>
  
  <p>I'm used to Objective C and Java where I can declare my variables in the beginning, and modify these variables anywhere in the code. </p>
  
  <p>I'm sorry if this is a basic question but I've been googling for hours and I just can't get it to work. </p>
  
  <p>Here is the code:</p>
  
  <pre><code>logstuff("outside");
  
  
  function logstuff(logInput){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
  var lastRow = sheet.getLastRow() + 1;
  sheet.getRange("A"+lastRow).setValue(logInput);
  return;
  }
  
  
  if (typeof(globalTestVar) == 'undefined') {
  logstuff('declaring global variable');
  globalTestVar = 0;
  
  } else {
  logstuff('global variable has been declared'); 
  }
  
  
  
  function globalVarTestUIFunction() {
  var app = UiApp.createApplication().setTitle('Test UI');
  var doc = SpreadsheetApp.getActive();
  var formPanel = app.createVerticalPanel();
  
  
  var buttonF1 = app.createButton('F1');
  var buttonbuttonF1ClickHandler = app.createServerClickHandler("globalVarTestFunctionOne");
  buttonF1.addClickHandler(buttonbuttonF1ClickHandler);
  buttonbuttonF1ClickHandler.addCallbackElement(formPanel);
  
  var buttonF2 = app.createButton('F2');
  var buttonbuttonF2ClickHandler = app.createServerClickHandler("globalVarTestFunctionTwo");
  buttonF2.addClickHandler(buttonbuttonF2ClickHandler);
  buttonbuttonF2ClickHandler.addCallbackElement(formPanel);
  
  
  app.add(formPanel);
  
  formPanel.add(buttonF1);
  formPanel.add(buttonF2);
  
  
  doc.show(app);
  
  return app;
  }
  
  
  
  function globalVarTestFunctionOne() {
  logstuff('globalVarTestFunctionOne');
  globalTestVar++;
  logstuff('Value of globalTestVar: ' + globalTestVar);
  }
  
  function globalVarTestFunctionTwo() {
  logstuff('globalVarTestFunctionTwo');
  globalTestVar++;
  logstuff('Value of globalTestVar: ' + globalTestVar);
  }
  </code></pre>
  
  <p>Output:</p>
  
  <ul>
  <li>outside3</li>
  <li>declaring global variable</li>
  <li>outside3</li>
  <li>declaring global variable</li>
  <li>globalVarTestFunctionOne</li>
  <li>Value of globalTestVar: 1</li>
  <li>outside3</li>
  <li>declaring global variable</li>
  <li>globalVarTestFunctionTwo</li>
  <li>Value of globalTestVar: 1</li>
  </ul>
  
  <p>I've written my own function "logstuff" to print out messages because I don't like the built in Logger.log-function.</p>
  
  <p>Thank you!</p>