Defining global variables in Google Apps Script that can be used in multiple projects

I have about 15-20 Google Apps Script projects that use the same list of global variables.

All I did is define all the global tables at the beginning of the first Script file in the project, and then copied and pasted the code block in the same place in each project. Therefore, if I make changes to one, I copy and paste the whole thing from this into the rest. It takes a lot of time.

Is there a better way to do this? Are Libraries Used ? Does anyone use libraries to define global objects by project?

+4
source share
1 answer

Using the library for common constants is the most efficient way to share persistent objects between Google Apps scripts. Some reservations:

  • All scripts using ConstLib will need to do this when the "Development Mode" is turned on, otherwise you will still have to manually update each of them. (Danger: save the wrong version of ConstLib and all your scripts will break immediately.)screenshot
  • Constants are library attributes, so you will need to be referenced using the library name, for example.

    var log = SpreadsheetApp.openById( ConstLib.auditLogId );
    

    In your existing scripts, you might find it convenient to change the block of existing constants in ConstLib links, so you don’t need to touch the remaining code. eg.

    var auditLogId = ConstLib.auditLogId;
       . . .
    var log = SpreadsheetApp.openById( auditLogId );
    

Example

Constlib

var roses = "Red", violets = "Blue";

Use constlib

function myFunction() {
  Logger.log(ConstLib.roses);
  Logger.log(ConstLib.violets);
}

[14-10-09 14:51:47:258 EDT] Red
[14-10-09 14:51:47:259 EDT] Blue
+8

All Articles