Fix Apps Script file order problems with Exports

20 views
Skip to first unread message

Bruce Mcpherson

unread,
Dec 2, 2022, 7:14:57 AM12/2/22
to Google Apps Script Community

It’s good practice to keep class and namespace definitions in separate files and avoid defining functions or variables in the global space. However, App Script doesn’t give you control over the order in which it loads files. If you reference a class or a namespace from one script file, it may not yet be defined. This is where an Exports object comes in.

Using ‘var’ rather ‘const’ can help, as JavaScript var ‘hoists‘ both var and function declarations, but this won’t always solve the problem, especially with class definitions.

Many of my projects have 20 or more files and I kept running into this problem until I figured out this simple workaround. You also create a much cleaner, better documented and more flexible project.

Here’s how to use an Exports object which documents the contents of your script

Michael O'Shaughnessy

unread,
Dec 3, 2022, 1:35:41 PM12/3/22
to google-apps-sc...@googlegroups.com
Once again you have blown my mind!!  What an elegant and efficient approach.  You have "ruined" my Saturday because now I have to "play" with this new approach to see how I can use it in my own solutions...😁 
(BTW: a day of coding is never "ruined"... the wife wanted me to put up Christmas decorations.... Oh well, that will have to wait!!!)

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/d3cab6d3-8623-45d9-aff3-35eddb4c37bbn%40googlegroups.com.

Marcos Gomes

unread,
Dec 3, 2022, 6:07:31 PM12/3/22
to google-apps-sc...@googlegroups.com
When I believe my level is improving, Bruce shows me how far I am.  😁


Reply all
Reply to author
Forward
0 new messages