Search code examples
google-apps-scriptmodulecode-organizationproject-organizationglobal-scope

GAS files: how to free global space from polution?


How do I clean up my global space without having to create +1000 lines .gs files

  • Here follows two photos of some of my files/folders. Mind how low the scroll bar can slide (30+ GAS files) ↓

Files and folders used in my project enter image description here

  • In Apps Script, .gs files cannot be manually mixed like .js modules, which means I cannot import or export (right?), neither control what will be shown when you search for completions;
  • If I wrap it around an interface, I usually end up with +1000 lines of code, which made the whole project difficult to read;
function groupOfFunctions() {
    this.doThis = function() { /* hundreds of lines */ };
    this.doThat = function() { /* hundreds of lines */ };
    // ...
    this.subFunc_N = function() { /* hundreds of lines */ };
    return this; // <-- makes tired face after this
};
  • To avoid that, the project's been organized in a way all Functions rest on the global space;
  • New problem: eventually, my global space has gotten heavily poluted with tons of Functions;

enter image description here enter image description here

  • The scenario is: I usually forget what Functions do, or even that they exist (so many times I coded something that was ready, but lost a
  • I've found no Youtube videos, questions in forums or anything like it anywhere on the internet that talks about how to deal with it, so the question is:

How do I clean up my global space without having to create +1000 lines .gs files

Articles, blogs, videos etc. are welcome.


Solution

  • Years later, after reviewing my own question, I conclude that:

    • There was no need for so many files, neither that much of functions. Most of the files were composed of Classes like 'Student, Lesson' that could lack in a project as simple as this one was. I've learned that a project must be as simple as it needs, and time should be spent smartly, with relevant tasks.
    • Proper closuring and the right amount of abstraction would certainly depollute global space. Apps Script's globalThis looks excessive, at first; however, usage has tought me that each item listed after pressing ctrl+space was carefully made easy, by design, because you'll often use them.
    • There's a point where less is more, especially when it comes to abstraction and inheritance. If it's easier and faster to do it manually on Google Sheets, then do it manually; if algorithm is needed, make it simple. Fancy patterns and Youtube videos might give you the wrong impression that you need to code a lot, when you don't.

    I used to write code like seen below:

    function getSpreadSheet() {//code...};
    function getStudentsSheet(){//code...};
    funcrion getStudentsDataRange(){
        return getStudentsSheet().getDataRange()}
    function getStudentsSheetValues(){
        return getStudentsSheetDataRange().getValues()};
    function logValues() {
        console.log( getStudentsSheetValues() );
    };
    

    While it could be simply:

    function logValues() {
        const id = '...'
        const values = SpreadsheetApp
            .openById(id)
            .getSheetByName('Students')
            .getDataRange().getValues();
        console.log(values);
    }