0

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

4
  • It sounds like you're looking for session variables, not global variables. I don't know Google App Scripts, so I don't know if it has this. Commented Feb 14, 2020 at 23:04
  • Looks like it does, the Session class. stackoverflow.com/questions/44573749/… Commented Feb 14, 2020 at 23:04
  • I think that in your script, when launch() is run under with and without enabling V8 at the script editor, Logger.log(globalVariable) in second() shows test. So can I ask you about the detail flow for replicating your issue of globalVariable gets logged as null? By the way, if you want to make users use the same variables, how about using getScriptProperties() instead of getUserProperties()? But I'm not sure about your actual situation. So if this was not the direction you want, I apologize. Commented Feb 14, 2020 at 23:41
  • You might be able to use UserProperties rather than ScriptProperties. Commented Feb 15, 2020 at 0:55

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Diego for your answer. I am very sorry for how unclear I was the first time I asked the question. I just updated my question and hopefully it's more clear now. I included a snippet of my actual code this time.
@DavidFahim Return the value of Gfamily to the frontend and use window.sessionStorage (mentioned above) to save it. Then pass that value into definer() whenever you call it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.