Execution order of GS files in a Project

2019-03-12 08:57发布

问题:

Where can I read documentation concerning the execution order rules for GS files?

To dimension the problem I created two trivial objects, each in their own file.

1_File.gs

var ObjB = new Object();
ObjB.sayName = "[" + ObjA.sayName + "]";

0_File.gs

var ObjA = new Object();
ObjA.sayName = " I'm A ";

A call such as ...

Logger.log(ObjA.sayName + " : " + ObjB.sayName);

... gets the error ...

TypeError: Cannot read property "sayName" from undefined.

If I move the code from 1_File.gs into 0_File.gs, and vice versa, then there is no error and the log shows correctly ...

I'm A : [ I'm A ]

Renaming 0_File.gs to 2_File.gs doesn't affect execution order either, so I assume that order depends on which file gets created first.

Is there no concept of "include" or "import" that would allow me to make order of execution explicit?

回答1:

Where can I read documentation concerning the execution order rules for GS files?

There is no such documentation and I think will not be any time published. In similar way, an initialization order of the static variables in C++ is also undefined and depends on compiler/linker.

Is there no concept of "include" or "import" that would allow me to make order of execution explicit?

Yes, there is no "includes", "imports" and even "modules", but there are libraries.

Also there is a workaround by using a closure. Bellow is a sample code. By executing the test function the log contains c.d. The idea is to have in all gs files a function started with init. In these functions all global variables are instanced. The anonymous closure is executed during the Code.gs file instancing and calls all "init" functions of all gs files.

Code.gs

var c;

function callAllInits_() {
  var keys = Object.keys(this);
  for (var i = 0; i < keys.length; i++) {
    var funcName = keys[i];
    if (funcName.indexOf("init") == 0) {
      this[funcName].call(this);
    }
  }
}

(function() {
  callAllInits_();
  c = { value : 'c.' + d.value };
})();

function test() {
  Logger.log(c.value);
}

d.gs

var d;

function initD() {
  d = { value : 'd' };
};


回答2:

If you have more than one level of inheritance, you need to give the init functions names like init000Foo, init010Bar, and init020Baz, and then sort the init functions by name before executing. This will ensure init000Foo gets evaluated first, then Bar, then Baz.

function callAllInits() {
  var keys = Object.keys(this);
  var inits = new Array();
  for (var i = 0; i < keys.length; i += 1) {
    var funcName = keys[i];
    if (funcName.indexOf("init") == 0) {
      inits.push(funcName);
    }
  }

  inits.sort();
  for (var i = 0; i < inits.length; i += 1) {
    // To see init order:
    // Logger.log("Initializing " + inits[i]);
    this[inits[i]].call(this);
  }
}


回答3:

There is no such order in Google Apps Script. It purely depends on where you have these objects declared and how your function is invoked. Can you explain a bit about how and when your Logger.log() code will be invoked. Also, when do you declare your objects objA and objB ? These will help us provide a better answer



回答4:

I tackled this problem by creating a class in each file and making sure that each class is instantiated in the original Code.gs (which I renamed to _init.gs). Instantiating each class acts as a form of include and makes sure everything is in place before executing anything.

_init.gs:

// These instances can now be referred to in all other files
var Abc  = new _Abc();
var Menu = new _Menu();
var Xyz  = new _Xyz();
var Etc  = new _Etc();

// We need the global context (this) in order to dynamically add functions to it
Menu.createGlobalFunctions(this);

function onInstall(e) {
  onOpen(e);
}

function onOpen(e) {
  Menu.build();
}

And classes usually look like this:

menu.gs:

function _Menu() {
  this.build = function() {
    ...
  }

  ...
}


回答5:

The other answers (i.e., don't write any top-level code which references objects in other files) describe the ideal way to avoid this problem. However, if you've already written a lot of code and rewriting it is not feasible, there is a workaround:

Google App Script appears to load code files in the order they were created. The oldest file first, followed by the next, and the most recently created file last. This is the order displayed in the editor when "Sort files alphabetically" is unchecked.

Thus, if you have the files in this order:

  • Code.gs
  • 1_File.gs (depends on 0_File.gs)
  • 0_File.gs

An easy fix is to make a copy of 1_File.gs and then delete the original, effectively moving it to the end of the list.

  1. Click the triangle next to 1_File.gs and select "Make a copy"
    • Code.gs
    • 1_File.gs
    • 0_File.gs
    • 1_File copy.gs
  2. Click the triangle next to 1_File.gs and select "Delete"
    • Code.gs
    • 0_File.gs
    • 1_File copy.gs
  3. Click the triangle next to 1_File copy.gs and select "Rename", then remove the " copy" from the end.
    • Code.gs
    • 0_File.gs
    • 1_File.gs

Now 0_File.gs is loaded before 1_File.gs.



回答6:

here is how I would do this...

main

function include(filename) {
  return ContentService.createTextOutput(filename);
}

function main() {
  include('Obj A');
  include('Obj B');
  Logger.log(ObjA.sayName + " : " + ObjB.sayName);
}

Obj A

var ObjA = new Object();
ObjA.sayName = " I'm A ";

Obj B

var ObjB = new Object();
ObjB.sayName = "[" + ObjA.sayName + "]";