1

I have a Javascript project which one of its script receive a parameter. Instead of passing this parameters throughout function chains can I declare it one as global and have other functions referring to it instead?

What if there are many scripts in the project? Can these access this global variable somehow? I need to have it persistent to the duration of the execution only.

Note that this project can be called by various users and at once. As it is sometimes invoked through a webapp I am not sure User cache would be appropriate.

Thanks!

3
  • What about scriptProperties? Commented Feb 9, 2015 at 19:14
  • Please try it before posting The question. Show what doesnt work (because it does) Commented Feb 9, 2015 at 20:02
  • @Kriggs: I have been using Script properties on some occasions but I want to keep some info for the duration of the call to the library only (the one that contains many scripts and functions). Had many problems with using user cache. For some reasons it hasn't been consistent but I can't afford to debug that code and again it keeps value beyond the duration of the call. Commented Feb 10, 2015 at 7:33

1 Answer 1

3

Don't use Global variables for Apps Script Services. For example:

var SS_SERVICE = SpreadsheetApp;

Recently this started causing an error message. If this changes, please edit the answer at that time.

Also, if you do not use the var keyword to define a variable, then it automatically gets put into the global scope. So, if by mistake, you fail to put var in front of your variable, then the code still runs and may work, but you may be unaware of what is really happening with your code. If you defined and used another variable with the same name in a different function, and also mistakenly made that variable a global, and one function called the other function, then there could be a conflict with the variable values.

All Apps Script .gs files can access all other .gs script files. There doesn't need to be any link between script files, or inclusion into other script files. You can call a function from another script file, as long as it's in the same project.

And global variables defined in one file are accessible to other files.

You don't want to use public Cache for information specific to that user. But there is private Cache. And Cache expires, so unless it's for something like timing how long a user is logged in, you might not want to use it.

If you have lots of code, and create functions for reasons of orderly structure and access to multiple other functions, then passing data might be undesirable. So, yes, you can use global variables. It's considered "Bad Practice" by some to use global variables, but then we are getting into personal opinion.

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

3 Comments

Thanks Sandy! It has been a very long while since I did programming (nearly 20 years ago) and Javascript is definitely not a language i was used to and thoroughly know all its quirks but I have learnt that what is 'undesirable' it is just a matter of proper discipline. Your answer helped me a lot and put me on the right track! Thanks again for a superb answer.
BTW Sandy. Just a question in regard to private cache. Are you referring to user cache? Do you happen to know whether when using a webapp that neither effective user nor active user return a value would the user cache work? I had quite a few challenges with it... Thx.
Good question. I don't know. I think you are referring to this question: Stack Overflow - Effective or Active Users

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.