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
Sessionclass. stackoverflow.com/questions/44573749/…launch()is run under with and without enabling V8 at the script editor,Logger.log(globalVariable)insecond()showstest. So can I ask you about the detail flow for replicating your issue ofglobalVariable gets logged as null? By the way, if you want to make users use the same variables, how about usinggetScriptProperties()instead ofgetUserProperties()? But I'm not sure about your actual situation. So if this was not the direction you want, I apologize.