Can you use global vars in GAZ? are there any disadvantages?

Sorry if this is a stupid question, but here goes

So, I've been learning Javascript for use with google app scripting for about a year now, and slowly but surely finding my feet.

I tried to help someone with their script here and I noticed that they declared

var ss = SpreadSheetApp.getActiveSpreadsheet();

      

at the very top of the script, outside of all functions, as a global var.

I thought that when I write multiple functions / scripts for a spreadsheet, it might be worth declaring some VARs globally rather than repeating them in different functions.

Before I blindly plow this path, I thought it might be better to ask if there are any major problems or issues with using global vars in GAS.

Also, are there any important benefits beyond saving a bit of input while coding?

Anyone currently writing GAS scripts using global vars regularly. I would be interested to know how does it all work? What are the disadvantages, any limitations or advantages.

EDIT BELOW THIS LINE

Just want to add that 95% of the things I've done have been limited to Google Sheets using lil gmail. So that's my coverage so far. Thought it was best to mention as I really have no scripting implications for other Google products in mind.

+3


source to share


2 answers


When you use a variable definition, for example in your example, that calls a Google service, you should be aware that this call will be made every time any function in your project is run .

This means that even if it is probably efficient in terms of writing code (we are all lazy guys, I think), it is really inefficient in terms of speed of execution.

Conversely, suppose you are executing a function that does not need a variable ss

, it will execute SpreadSheetApp.getActiveSpreadsheet()

...



With such a simple call, it might not be a problem, but if you multiply this case by many different variables calling many different services, it will ultimately slow down your execution speed.

I am not a "real" programmer, so my opinion is based only on personal habits and findings, as I have never learned anything "academic" in this matter. Maybe someone else will have a more relevant answer.

EDIT: While I was typing Jonathan, just did! I agree with what he said, of course, and I will leave my post here just for efficiency ;-)

+3


source


There is a purist look and a pragmatic look.

Pragmatically disadvantages you may face.

  • Naming conflict potential with any libraries you add
  • lose global values ​​defined in multiple project scripts
  • the variables calling the API endpoints will always run regardless of whether this instance of your script is needed or not.

I use globals, but in a slightly conservative way oppose both of them.



  • I create one global object that is uniquely named and then injects any global vars as properties of that object. Essentially, globals are then replaced with names.
  • I discipline myself to only define globals in one globals.GS per project.

The puristic concern about variable scoping is less than js in the wild, as scripts are somewhat isolated during execution. This is not the case when you publish libraries.

I am not defining globals like the example you give, but by binding a variable to a specific GAS API. There might be benefits out there, I don't know, but I tend to reuse code templates as functions between functions and prefer not to require me to copy any code outside of scoped functions myself. Also they will call these API endpoints whether your target function requires = rate limiting or not.

Globals for me are really script specific project variables and utility functions. They can often be script property calls.

+2


source







All Articles