- You want to use
userProperties
with the value, which is given in the function of onEdit
, at the function of onEditOfH3
, when onEdit
is run.
- You want to understand about the specification of the files in one GAS project.
If my understanding is correct, how about this answer? Please think of this as just one of several answers.
About all files in one GAS project:
At the project of Google Apps Script, all files in the project are used as one project. Namely, for example, when the following sample script is run in the file of Code.gs
,
function myFunction() {
for (var i in this) {
if (typeof this[i] == "function") {
Logger.log(i)
}
}
}
all functions in all files in the project are returned. From this, it is found that when a global variable is declared at the file of Code.gs
, this variable can be used at other file in the same project. At the following pattern 1, this is used.
Pattern 1:
In this pattern, userProperties
is declared as the global variable.
Modified script:
Code.gs
var userProperties; // This is declared as the global variable.
function onEdit(e){
totalHoursLoggedForStory_today = Number(e.range.getSheet().getRange("H3").getValue()) + Number(e.range.getSheet().getRange("H4").getValue()) + Number(e.range.getSheet().getRange("H5").getValue());
userProperties = PropertiesService.getUserProperties(); // Modified
var newProperties = {'hoursLogged': totalHoursLoggedForStory_today };
userProperties.setProperties(newProperties);
onEditOfH3(e);
}
23546.gs
This is not required to be modified.
Pattern 2:
In this pattern, userProperties
is added to the object of e
. And the value is used as e.userProperties
in the function of onEditOfH3
. If you don't want to use the global variable, how about this? Also, userProperties
can be sent as another argument.
Modified script:
Code.gs
function onEdit(e){
totalHoursLoggedForStory_today = Number(e.range.getSheet().getRange("H3").getValue()) + Number(e.range.getSheet().getRange("H4").getValue()) + Number(e.range.getSheet().getRange("H5").getValue());
var userProperties = PropertiesService.getUserProperties();
var newProperties = {'hoursLogged': totalHoursLoggedForStory_today };
userProperties.setProperties(newProperties);
e.userProperties = userProperties; // Added
onEditOfH3(e);
}
23546.gs
function onEditOfH3(e){
// var sh = SpreadsheetApp.getUi(); // In your whole script, this might be declared at elsewhere.
if (e.range.getA1Notation() == "H3") {
var temp = e.userProperties.getProperty('hoursLogged'); // Modified
sh.alert(temp);
}
}
Reference: