Why your top-level const resets every time
Apps Script does not keep a running process between executions. Every trigger fire, every manual run, every `doGet` call spins up a fresh V8 isolate, evaluates your files top to bottom, runs the target function, and then discards the entire runtime. A line like `const lastRow = 1;` at the top of your file is not a persistent value — it is an initializer that runs from scratch on every invocation. The first time I hit this, I spent an embarrassing amount of time adding `Logger.log` calls before realizing my variable wasn't drifting, it was being reset wholesale.
This is not a quirk you can work around with a global object or a module-level cache. There is no shared memory between executions. If a value needs to outlive the current run, it has to leave the runtime entirely and be stored somewhere durable before execution ends.