I want to pass a variable from one script to another in the same App Script project using Properties Service

I want to pass a variable from one script to another in the same App Script project using Properties Service
javascript
Ethan Jackson

The variable needs to be set programmatically and I have tried this, which appears to work in the donor script but not from other scripts. I am running the function setlastrow in the first program and getlastrow in subsequent programs. To reference the variable, I have to just call the function 'getlastrow' ?? The overall object is to get the number of rows in a sheet and to pass that to other scripts which run immediately following, but I need to ensure that the following scripts use the same number of rows whilst other updates may occur.

//Two functions to pass static variable LASTROW to f Create Accounts and f AutoRegWaitList to avoid changes to the LASTROW variable whist the 3 are running function setlastrow () { LASTROW = wsFormresponses.getLastRow(); PropertiesService.getScriptProperties().setProperty("lastrow", LASTROW); } function getlastrow () { var LASTROW = PropertiesService.getScriptProperties().getProperty("lastrow"); } Logger.log(getlastrow) ////////////////////////////////////////////

and called like this? for (var i=50;i<=getlastrow;i++) {

Answer

The issue is that getlastrow() doesn't return anything, so when you call it in your loop, it's undefined.

function getlastrow() { return Number(PropertiesService.getScriptProperties().getProperty("lastrow")); }
for (var i = 50; i <= getlastrow(); i++) { }

Make sure setlastrow() is called before the others run, so the value is stored properly.

Related Articles