-1

My apologies for the insane question but is it possible to have something like:

var data = ss.getSheetByName("info").getDataRange().getValues();

called as a global (ie, before using any functions) and then having that data var be available to all the functions? My program mainly reads through arrays with the main sheet having over 5k rows with mult columns of info and another sheet having almost 15k rows/mult columns of info.

Was wondering if it would be any saver of time by doing it this way instead of calling it every time the function is called.

I've been using arrays because I hear it's quicker for Google to flip through it find a particular piece of data then it is of Objects, etc. Sorry if I am making ZERO sense here ;)

Rubén
  • 34,714
  • 9
  • 70
  • 166
Michael
  • 33
  • 5
  • 1
    There are a lot of opinions about global variable, mostly bad. See this answer https://stackoverflow.com/questions/2613310/ive-heard-global-variables-are-bad-what-alternative-solution-should-i-use – TheWizEd Feb 07 '22 at 02:47
  • 1
    Related: https://stackoverflow.com/questions/65150194/how-to-use-global-variables-while-avoiding-permission-errors – TheMaster Feb 07 '22 at 13:18
  • 1
    If you make that a global it will run everytime you call any function,. So if it's a big sheet it will slow you down quite a bit. – Cooper Mar 26 '22 at 04:58

1 Answers1

2

Code in the global scope of a Google Apps Script is loaded on every execution, meaning that if you declared a variable in the global-scope it will be "overwritten" on every execution.

It might help if you have a "main" function that calls several helper functions that require the same object instance but this might have a "slightly" negative effects if you have functions that don't require that object instance. If your spreadsheet takes a lot of time to be loaded then the negative effects will not be "slightly", they will be noticeable, importantly or even dramatically negative.

Rubén
  • 34,714
  • 9
  • 70
  • 166