2

I have a project set up like this:

TestProject

-- Code.gs

-- 14885.gs

-- 23546.gs

I want to create a variable and set its value in "Code.gs" file. Then I should be able to access it in 14885.gs and 23456.gs files.

In Code.gs file, I used the PropertiesServices and created a variable that I wanted to access across other "gs" files under the project.

In Code.gs file


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); 

  onEditOfH3(e);
} 

In 23546.gs file


  function onEditOfH3(e){   
   if (e.range.getA1Notation() == "H3") 
    { 
    var temp= userProperties.getProperty('hoursLogged'); 
    sh.alert(temp);
    }

Expected: In the 23546.gs file, I should be able to alert the property that I set in "Code.gs" file.

Actual: I am seeing this error: ReferenceError: userProperties is not defined

Jrules80
  • 178
  • 12
  • Although I'm not sure whether this is the direct solution of your issue and the method for running the script of `In 23546.gs file`, if your script is modified, for example, how about putting `var userProperties = PropertiesService.getUserProperties();` to outside of the function of `onEdit(e)`? Or how about modifying from `var temp= userProperties.getProperty('nickname')` to `var temp= PropertiesService.getUserProperties().getProperty('nickname')`? – Tanaike Sep 04 '19 at 03:05
  • OK, I've modified my question a little bit. But, I must set the variable value in "onEdit()" function because I am retrieving some values from a few cells when the onEdit() function is invoked. – Jrules80 Sep 04 '19 at 03:16
  • Thank you for replying. In your updated script, it seems that when `onEdit()` is run, the value of `totalHoursLoggedForStory_today` is put to PropertiesService. 1. Can I ask you about the method for running the script of `In 23546.gs file`? 2. What is your current issue? – Tanaike Sep 04 '19 at 03:23
  • Sure. My apologies for not being clear in my question. I have modified my question ... again :). To answer your questions: 1. From onEdit(e) function in Code.gs file, I am calling a method in 23546.gs file called onEditOfH3(e) 2. The issue is, I am getting a "ReferenceError: userProperties is not defined" in 23546.gs logs. – Jrules80 Sep 04 '19 at 03:27
  • Thank you for replying and adding more information. I proposed 2 patterns for your situation. Could you please confirm it? If I misunderstood your question and those were not the result you want, I apologize. – Tanaike Sep 04 '19 at 05:00

2 Answers2

1
  • 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:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
0

Doesn't this:

function onEditOfH3(e){   
  if (e.range.getA1Notation() == "H3") { 
    var temp= userProperties.getProperty('hoursLogged'); 
    sh.alert(temp);
  }

have to be like this:

function onEditOfH3(e){   
  if (e.range.getA1Notation() == "H3") { 
    var temp= PropertiesService.getUserProperties().getProperty('hoursLogged'); 
    sh.alert(temp);
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Mr. Cooper, Thank you for pitching in. I appreciate your input. I have not had a chance to try your recommendation, but I will do it soon and report here. – Jrules80 Sep 05 '19 at 01:40