How to import functions in Google Apps Script
Asked Answered
G

3

24

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 ?

Garbe answered 27/2, 2014 at 3:51 Comment(0)
D
27

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

Declare answered 27/2, 2014 at 4:43 Comment(3)
where can we read about apps script libraries? What if all function come from another library?Scenic
Just as a precision, as Pablo LION precised it: You need to get the right order in the left side menu. ORDER DOES MATTERBag
Caveat: whether order of gs files matters appears to have changed (possibly more than once). As of June 2022 release notes: "Now, the order of files in the Apps Script editor doesn't matter. " developers.google.com/apps-script/releases#june_2022Neurology
S
17

Import is not needed. And

As of June 2022 release notes: "Now, the order of files in the Apps Script editor doesn't matter. "

Thanks to @pestophagous 's input

Outdated old answer

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.

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 anywhere. I myself have decided stop using GAS for this fact, and these reasons for myself.

Spaceman answered 24/7, 2021 at 4:9 Comment(9)
This should be the accepted answer.Microstructure
BTW there's a rumor that Google is not investing more on this.. So maybe just not learn it @PaulRazvanBergSpaceman
interesting. Do you mean Google is not investing in Apps Script, or what exactly?Microstructure
Correct. Tho it's just a rumor. But overall, Chrome Extensions has more capability than GAS (Google Apps Script). The only advantage of GAS to me is that it works "cross-browser".Spaceman
So how could I use a Chrome extension to add custom functionality to my Google Sheets? Deprecating GAS does not make sense to me ..Microstructure
Even if they are doing it, the process would be very long. And feel free to check this: AppSheetSpaceman
The GAS documentation is frustrating on this. Whether order of gs files matters appears to have changed (possibly more than once). As of June 2022 release notes: "Now, the order of files in the Apps Script editor doesn't matter. " developers.google.com/apps-script/releases#june_2022Neurology
I just ran into this using Moment and moment-timezone.js in an add-on and can confirm that order DOES matter, at least it does today. Previous deployments were working when tested, but the exact same code was failing when testing as HEAD... I finally came across this thread, moved Moment above moment-timezone.js and everything is back to normal. I wasted hours on this :-/Myopic
@Myopic Yea it's getting confusing like before someone decided the order doesn't matter and now it becomes important again. May your hours RIPSpaceman
C
0

"If both gs are in the same project, its already available"

Yes, BUT you need to name the functions correctly. The name of the function with a capital letter determines its availability. "MyFunction" will be available in all .gs files of the project, but "myFunction" will NOT be available in other files.

Contestant answered 14/5 at 8:8 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewCalyx

© 2022 - 2024 — McMap. All rights reserved.