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

Viewed 16506

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.

3 Answers

Yes, it's possible.

  1. 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

Related