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 functiononEdit
!= 5? - How can I change
m
in functiononOpen
and then makep
=m
(= 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, them
variable is equal to 5. After that theonEdit
function is called and the GAS creates a new instance of the code, in this instance theonOpen
function is not called and them
variable is equal toundefined
by default and this value is assigned to thep
.To achieve it, you have to use intermediate services like the Cache or the ScriptDB Services.