How change glob in one function to see it in anoth

2019-07-22 00:17发布

English is not my native language, big sorry for my mistakes

Here is my real problem

var k = 2;
var m;

function onOpen(){ // on event - Open spreadsheet
    m = 5;
}

function onEdit(){ // on event - Edit spreadsheet
    var n = k; // k = 2
    var p = m; // p = undefine (why?)
}
  • Why p is undefined?
  • Why is m in function onEdit != 5?
  • How can I change m in function onOpen and then make p = m (= 5)?

1条回答
戒情不戒烟
2楼-- · 2019-07-22 00:53
  • Why p is undefined?
  • Why is m in function onEdit != 5?

The GAS internals instances and initializes the code every time a function is called, i.e. the onOpen function is called during a spreadsheet is opened, GAS instances the code, the m variable is equal to 5. After that the onEdit function is called and the GAS creates a new instance of the code, in this instance the onOpen function is not called and the m variable is equal to undefined by default and this value is assigned to the p.

  • How can I change m in function onOpen and then make p = m (= 5)?

To achieve it, you have to use intermediate services like the Cache or the ScriptDB Services.

查看更多
登录 后发表回答