本文介绍了项目中GS文件的执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在哪里可以阅读关于GS文件的执行顺序规则的文档?



为了确定问题的范围,我创建了两个不重要的对象, p>

1_File.gs

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

0_File.gs

  var ObjA = new Object(); 
ObjA.sayName =我是A;

一次调用,如...

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

...出错...

  TypeError:无法从undefined读取属性sayName。 

如果我将1_File.gs中的代码移动到0_File.gs中,反之亦然,没有错误和日志显示正确...

将0_File.gs重命名为2_File.gs也不会影响执行顺序,所以我认为顺序取决于首先创建哪个文件。 b
$ b

是否没有包含或导入的概念,使我能够明确执行顺序?

解决




$ b
没有这样的文件,我认为不会随时发布。以类似的方式,C ++中静态变量的初始化顺序也是未定义的,并取决于编译器/链接器。

是的,没有includes 进口甚至模块,但有。



还有一个解决方法是使用闭包。波纹管是一个示例代码。通过执行测试函数,日志包含 c.d 。这个想法是在所有 gs 文件中都有一个以 init 开头的函数。在这些函数中,所有全局变量都是实例化的。匿名闭包在 Code.gs 文件实例期间执行,并调用所有 gs 文件的所有init函数。

Code.gs

  var C; 

函数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);



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

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

d.gs

  var d; 

函数initD(){
d = {value:'d'};
};


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 ...

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?

解决方案

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.

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' };
};

这篇关于项目中GS文件的执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 23:08