Can I use global variable instead of passing parameters? - google-apps-script

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!

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.

Related

How to make a function file in Ocatve with multiple functions

I know that you can make a function file in Octave in which the file name is the same as the function which defines one function, but I would like to define multiple functions in one file. Is there any way to do this, or do I need a separate file for each function.
In this answer I will assume that your main objective is a tidy workspace rather than explicitly a one-file requirement.
Let's get the one-file approach out of the way. You can create a script m-file (not a function m-file), and define a number of command-line functions there. The octave manual has a section on this. Here's an example:
% in file loadfunctionDefinitions.m
1; % statement with side-effect, to mark this file as a script. See docs.
function Out = Return1(); Out = 1; end
function Out = Return2(); Out = 2; end
% ... etc
% in your main octave session / main script:
X = Return1() + Return2();
However, this is generally not recommended. Especially if you would require matlab compatible code, since matlab introduced 'script-local functions' much later than octave, and decided to do it in a manner incompatible to the existing octave implementation: matlab expects script-local functions to be defined at the end of the script; octave expects them to be defined before first use. However, if you use normal function files, everything is fine.
While I appreciate the "I don't like a folder full of functions" sentiment, the one-function-per-file approach actually has a lot of benefits (especially if you program from the terminal, which makes a wealth of tools twice as useful). E.g. you can easily use grep to find which functions make use of a particular variable. Or compare changes in individual functions from different commits, etc.
Typically the problem is more one of having such function files littering the directory, when other important files are present, e.g. data etc, and having so many files in one place makes finding what you want hard to spot, and feels untidy. But rather than have a single file with command-line definitions, there are a number of other approaches you can take, which are probably also better from a programmatic point of view, e.g.:
Simply create a 'helper functions' folder, and add it to your path.
Use subfunctions in your main functions whenever this is appropriate, to minimize the number of unnecessary files
Use a private functions folder
Use a 'package directory', i.e. a folder starting with the character '+', which creates a namespace for the functions contained inside. E.g. ~/+MyFunctions/myfun.m would be accessed from ~/ via MyFunctions.myfun(), without having to add +MyFunctions to the path (in fact you're not supposed to).
Create a proper class directory, and make your functions methods of that class
The last option may also achieve a one-file solution, if you use a newer-style classdef based class, which allows you to define methods in the same file as the class definition. Note however that octave-support for classdef-defined classes is still somewhat limited.

how to pass a gensym to a function name in common lisp

I'm using Lucerne to build an api, and experimenting with generating those apis based off of a list (they're very simple endpoints). The problem is that the views lucerne uses are just functions, so if I pass (gensym) to the macro at runtime the name of that view function is just set to (gensym) (or it fails, can't remember which).
I'd like to dynamically name a set of those view functions in a macro that will be in a loop of some kind, each time the code that macro has expanded too is run the functions have a new name (so if I just use a standard gensym each time the macro was run each of the functions would always have the same name, and overwrite each other). Is there some way to do this?
You probably want to use intern to create "public" symbols (that the users will be calling by name) and make-symbol for "private" symbols (that are stored somewhere).

How do I track down the source definition of a custom hook event in a Mediawiki extension?

Here's an example:
https://phabricator.wikimedia.org/diffusion/EPFM/browse/master/?grep=BeforeFreeTextSubst
A Mediawiki extension where Hooks::run( 'PageForms::BeforeFreeTextSubst', ...) gets invoked but there's no other record or trace of where it's defined. If there was some mapping of strings/names to functions it would be registered somewhere else, and if it was a function name it should show up somewhere else.
I'm seeing this with a few other function hook events.
There isn't any "source definition" other than where the hook is run from. That is where the hook is defined; it may or may not actually be hooked onto anywhere. All the hook definition is is a name and a set of parameters that are passed to hook callbacks.
To help find out where the hook is actually used, you can use the (new) codesearch tool:
https://codesearch.wmflabs.org/extensions/?q=BeforeFreeTextSubst
(It looks like this one is not used by any extension that is in Wikimedia source control.)
Are you trying to find the functions that get called when the hook is run? The situation is a bit chaotic there. There are two mechanisms for defining hooks:
the $wgHooks global (this is the normal way of registering hooks);
and the Hooks::register method (sometimes used for registering hooks dynamically).
$wgHooks is normally set via the extension.json file, but can be set dynamically, too.
The quickest way to find out what hooks are registered is to run maintenance/shell.php and type in $wgHooks. This will miss hooks registered via the other method, and hooks which are conditionally registered (e.g. only for API calls), but it still works 99% of the time. Otherwise, you'll have to grep for it, as Sam said.

Best practice for writing equivalent powershell scripts and functions

I have a script that takes in multiple parameters, and that I've documented with proper help comments (e.g. .SYNOPSIS, .DESCRIPTION, .PARAMETER). Several different users in my organization are going to use this powershell script, some who know powershell and will call it from powershell with specific parameter values, and some who don't know powershell and will simply right-click on the script file in Windows Explorer and choose Run with PowerShell (so the parameters will use their default values).
My conundrum is what is the best way to do this in powershell without a bunch of duplicate code. The way I see it, these are my options:
1 - Just write a DoStuff.ps1 script that provides default values for all parameters. This allows it to be ran directly from Windows Explorer, but feels clunky for the powershell users that want to use it as a function from their own scripts, since instead of writing:
Do-Stuff param1 param1
they will be doing:
.\DoStuff.ps1 param1 param2
2 - Within DoStuff.ps1, move the operations it performs into a DoStuff function, and after the function declaration call the DoStuff function with the parameters passed into the script. This would still allow the script to be ran from Windows Explorer, as well as developers to dot source the script into their own scripts so they can access the function. The downside is that when the developers dot source the script, the script is going to call the function with the default parameters (unless I allow them to provide an optional Switch parameter to the script that triggers the function to not be called). Even with this though, it means that I would have to duplicate all of the scripts help text so that it shows for both the script and the function (description, parameter descriptions, etc.).
I can't think of any other options. Ideally I would just be able to write functions in .ps1 file and tag a function with a "default" keyword so that if the script is called, that function is ran by default; but I don't think PowerShell provides anything like this.
What do you think is the best approach in this situation. Is there something I'm overlooking or don't know about? Thanks.
but feels clunky for the powershell users that want to use it as a function from their own scripts
Default parameters would seem, based on your description, to be the best (or, at least, least-worse) approach.
But rather than naming your script DoStuff.ps1 name it and call it so it can be called more like an internal function:
Name it with the dash: Do-Stuff.ps1
Remember you don't need to specify the ps1
If the script is in a folder in $env:Path then you don't need to specify a path.
Also consider a script can load a module from a relative path: you could put most of the code in a script module which the front end (right click on it) script loads and calls into it. Script authors load the module themselves.

Organizing Spreadsheet Code in several *.gs files - even possible?

I am trying to organize my code for a Spreadsheet in several script files. Within the script editor I can create as many *.gs files as I want, but I can't figure out how to access code that would be defined in another script.
Simple Example of what I'd like do achieve:
Code.gs:
function onEdit(){
myFunctionFromLibrary_gs();
}
Library.gs:
function myFunctionFromLibrary_gs(){
Browser.msgBox("hi there");
}
The onEdit() is obviously called by a Trigger.
Without modification this will result in a Runtime-Error, stating that
myFunctionFromLibrary_gs TypeError: is not a function, it is undefined.
So how can I make this work, or is this currently not supported?
Thx in advance for your help.
Yes, it's possible.
You are not limited to a single server Code.gs file. You can spread server code across multiple files for ease of development. All of the server files are loaded into the same global namespace, so use JavaScript classes when you want to provide safe encapsulation.
Reference: Google Documentation - features and limitations
I don't know what the _gs suffix means for Google, but without it (see code bellow), the code works.
file1.gs:
function onEdit(){
myFunctionFromLibrary();
}
file2.gs
function myFunctionFromLibrary(){
Browser.msgBox("hi there");
}
I know this is an old question but I found it looking for a similar task and happened to find the answer during my same search.
From the docs at https://developers.google.com/apps-script/guide_libraries#writingLibrary:
If you want one or more methods of your script to not be visible (nor usable) to your library users, you can end the name of the method with an underscore. For example, myPrivateMethod_().
While your function does not END in an underscore, it may have special meaning in other places than just this, or the _gs suffix may also have special meaning (particularly given the same filename suffix).