Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import functions in Google Apps Script

I have two Apps Script Code.gs and Code2.gs

I would like to import function A in Code2.gs and use it in function B in Code.gs.

Any ideas ?

like image 900
Vinay Joseph Avatar asked Feb 27 '14 03:02

Vinay Joseph


2 Answers

If both gs are in the same project, its already available. Else read about apps script libraries.

like image 158
Zig Mandel Avatar answered Nov 05 '22 11:11

Zig Mandel


Import is not needed. Put the function-defined-here.gs before function-used-here.gs in the list on the left, and call the function. The order DOES matter.

TLDR

In addition to the answer from @Zig Mandel

If both gs are in the same project, its already available. Else read about apps script libraries.

Files in the same project are executed by order in the list (in the new editor you can change the order) like importing a bunch of javascript with <script> tag into an HTML. So you will almost ways want the Code.gs be the last one in the list (the most dependent one). Also, names are irrelevant since all callback functions are added to the namespace, and later being called by the name from manifest file.

Fact: I searched the document about the fact I mentioned and it is not mentioned anywhwere. I myself have decided stop using GAS for this fact, and these reasons.

like image 39
Pablion Avatar answered Nov 05 '22 11:11

Pablion