David Fahim
David Fahim

Reputation: 129

Global Variable Bug in Google Apps Script Server Side

I am trying to create global variables in Apps Script and avoid built-in functions such as UserProperties. The reason for avoiding UserProperties is because all of my web app users will share the same variables which will result in their accounts being mixed together. I want to create global variables that have a lifetime until you close the tab/window. I can't use user properties because I need to run the script as myself as I am searching within a spreadsheet under my account in this web app.

function extractData(row){
  extractEmail(row);
  extractPoints(row);
  extractUsername(row);
  extractName(row);
  extractType(row);
  extractFamily(row);
  return "success";
}

function extractEmail(row){
  try{
    var url = "https://docs.google.com/spreadsheets/d/1pIC6Lyx4Q0ZjEA5GURZ3gA5qLmUJ0_7yGPJQmr6-GnQ/edit?usp=sharing";
    var sheet = SpreadsheetApp.openByUrl(url);
    var subsheet = sheet.getSheetByName("Accounts");
    var col = 2
    Gemail = String(subsheet.getRange(Number(row), col).getValue());
  }
  catch(error){
    Logger.log(error.message);
    }
}

function extractPoints(row){
  var url = "https://docs.google.com/spreadsheets/d/1pIC6Lyx4Q0ZjEA5GURZ3gA5qLmUJ0_7yGPJQmr6-GnQ/edit?usp=sharing";
  var sheet = SpreadsheetApp.openByUrl(url);
  var subsheet = sheet.getSheetByName("Accounts");
  var col = 6;
  Gpoints = Number(subsheet.getRange(Number(row), col).getValue());
}

function extractUsername(row){
  var url = "https://docs.google.com/spreadsheets/d/1pIC6Lyx4Q0ZjEA5GURZ3gA5qLmUJ0_7yGPJQmr6-GnQ/edit?usp=sharing";
  var sheet = SpreadsheetApp.openByUrl(url);
  var subsheet = sheet.getSheetByName("Accounts");
  var col = 4;
  Gusername = String(subsheet.getRange(row, col).getValue());
}

function extractName(row){
  var url = "https://docs.google.com/spreadsheets/d/1pIC6Lyx4Q0ZjEA5GURZ3gA5qLmUJ0_7yGPJQmr6-GnQ/edit?usp=sharing";
  var sheet = SpreadsheetApp.openByUrl(url);
  var subsheet = sheet.getSheetByName("Accounts");
  var col = 3;
  Gname = String(subsheet.getRange(row, col).getValue());
}

function extractType(row){
  var url = "https://docs.google.com/spreadsheets/d/1pIC6Lyx4Q0ZjEA5GURZ3gA5qLmUJ0_7yGPJQmr6-GnQ/edit?usp=sharing";
  var sheet = SpreadsheetApp.openByUrl(url);
  var subsheet = sheet.getSheetByName("Accounts");
  var col = 7;
  Gtype = String(subsheet.getRange(row, col).getValue());
}

function extractFamily(row){
  var url = "https://docs.google.com/spreadsheets/d/1pIC6Lyx4Q0ZjEA5GURZ3gA5qLmUJ0_7yGPJQmr6-GnQ/edit?usp=sharing";
  var sheet = SpreadsheetApp.openByUrl(url);
  var subsheet = sheet.getSheetByName("Accounts");
  var col = 12;
  Gfamily = String(subsheet.getRange(row, col).getValue());
  Logger.log(Gfamily);
}

function definer(){
  try{
  Utilities.sleep(5000);
  Logger.log(Gfamily);
  var data = [Gemail, Gname, Gusername, Gpoints, Gtype, Gfamily];
  return data;
  }
  catch(error){
    Logger.log(error.message);
  }
}

The code above is the server-side. Each function is called by the client-side along with the row already defined. The first function called by the client-side is extractData. When the global variable Gfamily is logged, it comes out correctly as testers. After the next function: definer is called from the client-side, Gfamily is logged as null. Please tell me how I can improve this question if I was unclear about anything. Below is a screenshot of the logs: Screenshot: Stackdriver logs showing how Gfamily suddenly becomes undefined

Upvotes: 0

Views: 204

Answers (1)

Diego
Diego

Reputation: 9581

Your frontend (browser) session is separate from your backend (server) session. Google's server isn't really tracking whether a user closes the tab/window, so you're going to have to manage the session from the frontend and then write your own server functions to correctly identify sessions.

To share a value across multiple users, I recommend using the Script Properties or Script Cache. Values stored in the script properties/cache will be available to all users of your web app. If one user updates the value, the new value will be immediately available to all other users. I recommend using this with Window.sessionStorage, which will clear automatically when the page session ends. So you could pass session data to the server and then manually track when a session is ended.

However, I don't really understand how you intend to use a shared value dependent on multiple user sessions. If one of the users ends their session, what happens to the shared value?

That makes me think there may be some misinterpretation, particularly due to your sentence

The reason for avoiding UserProperties is because all of my web app users will share the same variables which will result in their accounts being mixed together.

User properties are not shared, unless you deployed the app to "Execute the app as me". If you did that, then the script would always be executing under the same (your) user account and all user properties would be shared. But if you were to simply set "Execute the app as user accessing the web app", then you could safely use User properties/cache without any worries about mixed accounts. (The intention of User properties/cache is to avoid scope issues like mixing across accounts – take a look at this comparison table.)

Upvotes: 1

Related Questions