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 ?

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

Comments

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.