how to pass a dynamic variable from one function to another in Google App Script

101 Views Asked by At

Google sheets link: https://docs.google.com/spreadsheets/d/1oF_fM545_Vl_gCaliERs3bVjCfpz5oROstD_ndQAkjE/edit?usp=sharing

Google forms link: https://docs.google.com/forms/d/e/1FAIpQLSfom_KlWTWvPQDZF5mO4YTgwkznnB0LykMXibDrpfc-JBh-OA/viewform

App script link: https://script.google.com/u/0/home/projects/1uyj_Fm9yNePkUTiuvc-cdwkxPNZJwGPHMEig9-1Ufn01JWPZvZynfcxk/edit

Im getting an object Object when declaring a variable in the second function and consequently a null value when extracting the script properties.

The code currently:

  1. runs upon a form submission
  2. extracts the timestamp from the response as well as the email question
  3. stores in the script properties the timestamp property with the email value
  4. creates a time based trigger to run the firstLetter function

The problem comes in the firstLetter function, as the Logger.log(propertyKey) returns timestamp_[object Object] and the Logger.log(email) returns null. I believe its because the firstLetter function isnt getting the timestamp variable, which really has to do with Javascript scoping restrictions.

But I need the firstLetter function to run after the formResponse function (that is whay I created the trigger) and I need timestamp to be dynamic (not sure if thats the correct way to say it) but what I mean is I need the firstLetter function to only extract the script property from the corresponding response it was triggered by because multiple people can be submitting form responses.

Here is the code:

function formResponse(e) {
  var timestamp = e.values[0];
  var email = e.values[1];

  var propertyKey = 'timestamp_' + timestamp;
  PropertiesService.getScriptProperties().setProperty(propertyKey, email);

  ScriptApp.newTrigger('firstLetter')
  .timeBased()
  .after(10000)
  .create();
}

function firstLetter(timestamp) {
  var propertyKey = 'timestamp_' + timestamp;
  Logger.log(propertyKey)
  var email = PropertiesService.getScriptProperties().getProperty(propertyKey);
  Logger.log(email)

  if (email) {
    var firstL = email.charAt(0);

    Logger.log('First letter of email: ' + firstL);

    PropertiesService.getScriptProperties().deleteProperty(propertyKey);
  } else {
    Logger.log('Email not found for the given timestamp.');
  }
}
1

There are 1 best solutions below

2
cnavar On

You can try this:

function formResponse(e) {
  var timestamp = e.values[0];
  var email = e.values[1];

  var propertyKey = 'timestamp_' + timestamp;
  PropertiesService.getScriptProperties().setProperty(propertyKey, email);

  Utilities.sleep(10000);
  firstLetter(propertyKey);
}

function firstLetter(propertyKey) {
  Logger.log(propertyKey)
  var email = PropertiesService.getScriptProperties().getProperty(propertyKey);
  Logger.log(email)

  if (email) {
    var firstL = email.charAt(0);

    Logger.log('First letter of email: ' + firstL);

    PropertiesService.getScriptProperties().deleteProperty(propertyKey);
  } else {
    Logger.log('Email not found for the given timestamp.');
  }
}

I used Utilities.sleep to ensure that the firstLetter will only run after 10 seconds. Here's the result after testing this code:

enter image description here