22

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 ?

Vinay Joseph
  • 5,515
  • 10
  • 54
  • 94

2 Answers2

23

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

Zig Mandel
  • 19,571
  • 5
  • 26
  • 36
  • 1
    where can we read about apps script libraries? What if all function come from another library? – layser Jan 15 '22 at 13:56
  • Just as a precision, as Pablo LION precised it: You need to get the right order in the left side menu. *ORDER DOES MATTER* – Sami Boudoukha Aug 02 '22 at 15:54
  • 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. " https://developers.google.com/apps-script/releases#june_2022 – pestophagous Jul 05 '23 at 13:59
14

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.

Pablo LION
  • 1,294
  • 6
  • 20
  • This should be the accepted answer. – Paul Razvan Berg Mar 06 '22 at 20:03
  • 1
    BTW there's a rumor that Google is not investing more on this.. So maybe just not learn it @PaulRazvanBerg – Pablo LION Mar 07 '22 at 21:18
  • interesting. Do you mean Google is not investing in Apps Script, or what exactly? – Paul Razvan Berg Mar 08 '22 at 09:52
  • 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". – Pablo LION Mar 08 '22 at 22:29
  • So how could I use a Chrome extension to add custom functionality to my Google Sheets? Deprecating GAS does not make sense to me .. – Paul Razvan Berg Mar 08 '22 at 22:33
  • Even if they are doing it, the process would be very long. And feel free to check this: [AppSheet](https://about.appsheet.com/) – Pablo LION Mar 08 '22 at 22:42
  • 1
    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. " https://developers.google.com/apps-script/releases#june_2022 – pestophagous Jul 05 '23 at 14:01
  • 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 :-/ – jazzwhistle Aug 30 '23 at 00:25