Using Global vars in GAS, is it advisable? are the

2019-02-19 01:17发布

Sorry if this a stupid question, but here goes

So I've been learning Javascript for use with Google Application Scripts for about a year now and slowly but surely am finding my feet.

I tried to help someone with their script on here, and i notice that they declared

var ss = SpreadSheetApp.getActiveSpreadsheet();

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

It got me thinking, that when I write several functions/scripts for a spreadsheet, it might be worth declaring some VARs globally rather than repeating them in different functions.

Before I blindly plough down this path, I thought it best to ask are there any major pitfalls or problems with using global vars in GAS.

Also, are there any major advantages, besides saving a bit of typing while coding?

Does anyone currently write GAS scripts, regularly using global vars. I'd be interested to hear how it all works? What the cons are, any limitations, or advantages.

EDIT BELOW THIS LINE

Just wanted to add, 95% of the things I've been doing have been confined to Google Sheets, with a lil gmail scripting. So that's my scope so far. Thought it best to mention, as I don't really have the implications for scripts for other Google products in mind.

2条回答
太酷不给撩
2楼-- · 2019-02-19 01:37

There is a purist view and a pragmatic view.

Pragmatically the disadvantages you may face

  • naming conflict potential with any libraries you attach
  • losing track of globals defined in multiple scripts in a project
  • variables calling API endpoints will always run whether that instance of your script requires them or not.

I do use globals, but in a slightly conservative way to counter both of these.

  • I create a single global object that is uniquely named and then hang any global vars as properties of that object. Essentially globals are then namespaced.
  • I discipline myself to define globals only in a single globals.GS per project.

Puristic concerns about variable scope matter less than js in the wild as the scripts are somewhat sandboxed at runtime. This is not the case when you are publishing libraries however.

I do not define globals like the example you give however tying a variable to a specific GAS API. There maybe speed advantages to doing it, I don't know, but I tend to reuse code patterns as functions between functions and prefer to not require myself to copy over any code outside of scoped functions when doing so. Also they will call those API endpoints whether your target function requires them or not = speed penalty.

Globals for me are only truly script project specific variables and utility functions. Oftentimes they could be calls to script properties.

查看更多
男人必须洒脱
3楼-- · 2019-02-19 01:56

When you use a variable definition such as in your example that calls a Google Service you have to be conscious that this call will be executed each time you run any function in your project.

This means that even if it's probably efficient in terms of code writing (we are all lazy guys I guess) it is really not efficient in terms of execution speed.

On the contrary, let's imagine you execute a function that does not need the ss variable, the SpreadSheetApp.getActiveSpreadsheet() will be executed...

With a simple call like that it might not be an issue but if you multiply this case for a lot of different variables calling many different services it will finally slow down your execution speed.

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

EDIT : while I was typing Jonathon just did ! I do agree with what he said of course and I'll leave my post here just for the efficiency stuff ;-)

查看更多
登录 后发表回答