2

I have a script that I use as a library loading it in the spreadsheets I need.

When I add it to a spreadsheet, I define an onEdit and onLoad function that are calling the library's corresponding methods.

I need my individual sheets that are using the library, to define a global variable that should be available to the library, that variable may not be set in a sheet's cell.

I tried setting

var previd = "myid";

at the beginning of the spreadsheet script but that doesn't seem to do the trick.

How may I solve that ?

Rubén
  • 34,714
  • 9
  • 70
  • 166
Laurent
  • 1,048
  • 11
  • 23

1 Answers1

6

The "global scope" is not shared between scripts, it is actually a "script scope".

All variables needed for your library functions must be passed as parameters to these functions. If you make many functions call with the same parameters you should consider wrapping them in an object. Like some Apps Script built-in functions have optArguments.

You may also have a setParameters function on your library to pass these variables once (per runtime) and have them available to next library calls. Something like this:

//on the library script
var clientParams = null;
function setParameters(parameters) { clientParams = parameters; }
function exampleFunc() { return clientParams.previd; }


//on your client script
library.setParameters({previd:'myid', otherParam:'example'});

//then you can call
library.exampleFunc();
Henrique G. Abreu
  • 17,406
  • 3
  • 56
  • 65