Sort table code in multiple .gs files - is it even possible?

I am trying to organize my code for a table in multiple script files. In the script editor, I can create as many .gs files as I want, but I can’t figure out how to access the code that will be defined in another script.

A simple example of what I would like to achieve:

Code.gs:

function onEdit(){ myFunctionFromLibrary_gs(); } 

Library.gs:

 function myFunctionFromLibrary_gs(){ Browser.msgBox("hi there"); } 

OnEdit () is obviously triggered by a trigger. Unchanged, this will result in a Runtime-error, indicating that

myFunctionFromLibrary_gs TypeError: is not a function, it is undefined.

So how can I do this work, or is it currently not supported?

Thanks in advance for your help.

+6
source share
3 answers

I don’t know what the _gs suffix _gs for Google, but without it (see the code below) the code works.

file1.gs:

 function onEdit(){ myFunctionFromLibrary(); } 

file2.gs

 function myFunctionFromLibrary(){ Browser.msgBox("hi there"); } 
+7
source

Yes it is possible.

  1. You are not limited to one Code.gs server file. . You can distribute server code for multiple files for ease of development. All server files are uploaded to the same global namespace , so use JavaScript classes if you want to provide secure encapsulation.

Link: Google Documentation - Features and Limitations

+7
source

I know this is an old question, but I found that he was looking for a similar problem and accidentally found the answer during my search. From the docs at https://developers.google.com/apps-script/guide_libraries#writingLibrary :

If you want one or more methods of your script not to be visible (and not used) to users of your library, you can end the method name with an underscore. For example, myPrivateMethod _ ().

While your function does not match the END in the underscore, it may have special meaning in other places than just that, or the _gs suffix may also have special meaning (especially with the same suffix for the file name).

+2
source

Source: https://habr.com/ru/post/921692/


All Articles