Franco May
Franco May

Reputation: 3

code outside functions not remembered and running every time

I dont have a LOT of experience programming... so here is my doubt. I have written a large onEdit function on a google spreadsheet and placed some code outside that function, so it doesn't have to go through that code every time I edit a cell. its just variables containing the different sheeets, some important ranges, etc. I just want it to do run that script once and remember it. the issue is that every time I edit something, that code reruns. I tried writing an "onOpen" function, but the variables created inside that function don't have scope outside the onOpen function. I also tried declaring the variables with no value, and then adding the values in an onOpen function that only runs once. it too doesn't work. am I doing something wrong or is this just the way it works?

thank you!!

PS. I guess that you don't need the code because my questions goes beyond that. oh. and Im sorry for my english... Im not native.

Upvotes: 0

Views: 168

Answers (1)

IMTheNachoMan
IMTheNachoMan

Reputation: 5839

To add to what @TheMaster said, Google Apps Script is stateless. Meaning each invocation has to process the whole project. So when your onEdit function runs, it processes everything. For example:

var global = "hi";

function something(){ ... }

function onEdit()
{
    Logger.log(global);
    global = "bye";
}

something();

Every time the onEdit function triggers, it will run through all of the code. In this example, every time onEdit triggers, it'll set global to hi, run something(), and then run onEdit().

I hope that make sense?

Upvotes: 1

Related Questions