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