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:
- runs upon a form submission
- extracts the timestamp from the response as well as the email question
- stores in the script properties the timestamp property with the email value
- creates a time based trigger to run the
firstLetterfunction
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.');
}
}
You can try this:
I used
Utilities.sleepto ensure that thefirstLetterwill only run after 10 seconds. Here's the result after testing this code: