Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 401
leostone Avatar asked Jul 30 '12 16:07

leostone


People also ask

Can you have multiple scripts in Google Sheets?

You can have multiple scripts in the same script file and you can have multiple script files as well. If you click on the small downward-facing arrow at the right of the Script file name, it will show you options to rename, delete, and create a copy of the script file.

What is an active spreadsheet?

The active sheet in a spreadsheet is the sheet that is being displayed in the spreadsheet UI.

What is a macro in sheets?

A macro is a series of recorded actions within Google Sheets. Once recorded, you can activate a macro to repeat those actions later with a menu item or shortcut key. You can both create and update your own macros in both Google Sheets and the Apps Script code editor.

How do I create a spreadsheet in Google script?

To create a spreadsheet, use the create method on the spreadsheets collection, as shown in the following example. This example creates a blank spreadsheet with a specified title. // the built-in method SpreadsheetApp. create() is more appropriate.


2 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

like image 171
gihanchanuka Avatar answered Oct 18 '22 09:10

gihanchanuka


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

file1.gs:

function onEdit(){
   myFunctionFromLibrary();
}

file2.gs

function myFunctionFromLibrary(){
   Browser.msgBox("hi there");
}
like image 40
megabyte1024 Avatar answered Oct 18 '22 08:10

megabyte1024