onFormSubmit(e) returning NULL e value - google-apps-script

I am trying to work with data submitted by users via Google Forms. My trigger is tied to the spreadsheet on Form Submit, but when I use the following code, I get "e" returned with a NULL value and I can't do anything with it. I am submitting it through the forms and not just running a script. My spreadsheet is updating fine, but I can do anything with the values I'm getting.
function getTickets(e) {
var values = e.namedValues;
Logger.log(e);
Logger.log(values);
}
The exact error I get is:
[20-03-10 21:48:22:765 EDT] TypeError: Cannot read property 'namedValues' of undefined
at getTickets(Tickets:2:18)

The problem was reported that user was getting a nulll for event object parameter values. I suggested trying to switch back to legacy ES5 because it’s easy to do and it seems to fix many difficult to understand problems.

Related

Need Script for Google Docs to Send Auto Email

I'm looking for a script that I can add to a Google sheet that will auto generate an email and include some of the fields in the spread sheet.
I had created a Google Form and I have that data going to the Google spreadsheet, the idea is when the user submits the form it sends that data to the spreadsheet and the spreadsheet sends an automated email.
I found this script and edited it some but it fails on the 4th line (var theEvent = e.values[1]):
function AutoConfirmation(e){
var theirFirst = "Bill";
var theirEmail = johndoe#example.com;
var theEvent = e.values[1];
var subject = "Form Submitted";
var message = "Thank you, " + theirFirst + " for the expressed interest in our " + theEvent;
MailApp.sendEmail (theirEmail, subject, message);
}
Shouldn't line 4 pull the data from column 1 in my google sheet? Is this old an script and it doesn't work now?
In my google sheet I have Site instead of event as a column and another that has Complete as a header.
Let me know what I have missed here as it seems that this should be simple.
I tried to run this and this is the result: screenshot of the error I get
I get the same type of error when running the code above so I thought I would run a logger to see if I get anything with that and the result is in the screen shot. Click the link to see it.
It looks that the script that you found is function to be put in a Apps Script project bounded to a Google spreadsheet and called by an installable trigger.
Shouldn't line 4 pull the data from column 1 in my google sheet?
No. e.values returns an Array which use a zero based index. This means that index for Column A, the first column, is 0.
Issue:
The error you are getting:
TypeError: Cannot read property 'values' of undefined
Means that the event object (e) is undefined, which means that you are trying to run this function manually. Functions that are attached to a trigger are supposed run when the corresponding trigger event happens: in this case, when a user submits the Form. You don't have to run it manually.
Solution:
Step 1: Install the trigger: If you haven't done so yet, install the trigger, either manually, following these steps, or programmatically, by copy the following function to your bound script and running it once:
function createOnFormSubmitTrigger() {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger("AutoConfirmation")
.forSpreadsheet(ss)
.onFormSubmit()
.create();
}
Step 2: Submit the form!: Once the trigger is installed, when a user submits the Form that is attached to your Spreadsheet (that is, assuming that you have attached the Form to the Spreadsheet), AutoConfirmation runs automatically, and the event object, containing these properties, is passed as an argument. If you run it manually, e is undefined (no event object is passed as parameter), and you get the error.
Note:
e.values[1] will retrieve the same value that is written to column B when the form is submitted, since JavaScript arrays are zero-indexed. You might want to use e.namedValues['yourProperty'] instead, to make sure your are retrieving the desired information.
Reference:
SpreadsheetTriggerBuilder.onFormSubmit()
Event Objects: Form submit

Keep getting 'Null' when pulling data from google sheets

Whenever I try to pull data from 2 of 3 sheets from using the 'google.script.run' function call from Javascript, I keep getting an error saying the array I am returning is Null, but when I just change the exact same function call to work on another sheet, it returns the data perfectly
I have tried deleting the sheets and giving it the same names, I have tried using 'openWithURL' instead of 'getActive' to access the spreadsheet, I have tried rewriting the code, I have tried the same code in a different project, and checking the documentation to make sure I am not missing any detail. I have tried changing the references to the sheets, some work and some dont.
var SS = SpreadsheetApp.getActive();
var DB_BOOKINGS = SS.getSheetByName("BookingDatabase");
var DB_VEHICLES = SS.getSheetByName("VehicleDatabase");
var DB_REQUESTS = SS.getSheetByName("RequestDatabase");
function getRequestData(){
return DB_REQUESTS.getDataRange().getValues();
}
<script>
function getRequestData(callingFunction) {
google.script.run
.withSuccessHandler(callingFunction)
.withFailureHandler(CustomAlert)
.getRequestData();
}
</script>
I want to retrieve the sheet data but keep getting a null value
Since this is an issue with formatting as you said, try using getDisplayValues() rather than getValues(), this will pull the data as you see it in the sheet (as a string), rather than the unformatted data itself.
Reference:
getDisplayValues
I was having a similar problem. This was exactly the solution I needed. For my situation, I was able to use getValues() successfully on the initial page load, but when I tried to run it again as a sort of 'refresh' to update the values without reloading the entire page, it would return null.
My data did indeed contain dates, so after changing it to getDisplayValues(), it worked perfectly.

Catching #REF errors in Google Apps Script

I am writing a program in the Google Spreadsheet API for Google App Script which references the display value cells in a sheet to formulate QUERY() functions, then returns the result of that formulated QUERY() function to different cells.
Because there are a lot of changing variables when formulating these query functions, (I am referencing a cell responsible for the source sheet, for the column to look for, and the data point to return,) the query functions which are formulated often have #REF or #N/A errors. Although this is easily avoidable for me, the program is intended for a client and I, therefore, have to have better-informed error messages so someone can easily figure out the issue.
In this case here, I am referencing the sheet "Utility Companies" for the column "Utility Name," then returning the corresponding "Address" value. because "Error" doesn't appear in the "Utility Name" column in the "Utility Companies" sheet, a #N/A error is thrown when trying to return the corresponding address.
I tried a try-catch statement, but it didn't seem to recognize an error:
try{
//This is assigning the assembled QUERY() formula to a cell
//(works successfully & reliably)
retrieveRange.getRange().getCell(k+1, 1).setFormula(formulaArray[k]);
} catch (e){
//Never catches an error, and therefor never logs the following-
Logger.log("Error Recognized:" + e);
}
How do I catch these errors? And furthermore, how do I report a custom error message for those errors?
Thank you so much!

Google Form Submission Values are Undefined

I'm trying to capture some values from a form submission in a function and do some processing. So far, I'm having trouble just capturing values.
I have a very simple form set up with one field. I have the following code to pull the values and put them onto the Log.
function myFunction(e) {
var val1 = e.values;
Logger.log(val1);
}
I have set up the appropriate triggers such that the script runs whenever I submit a form, however, I always end up logging undefined.
[14-10-27 21:58:16:451 EDT] undefined
I've done this before, so I'm wondering if I'm missing a step or if something is wrong with my setup.
Thanks.
You need to have the function:
function onFormSubmit(e)

automatically create google contact from my google form

I have a google form that my clients fill out. How do I have the form create a google contact automatically each time someone new fills out the survey? I dont want to have to copy paste each time just to create a new contact in my gmail
I'm trying to use this script but getting an error when I try to run it with some test data
function onFormSubmit(e) {
var timestamp = e.values[0];
var firstName= e.values[1];
var lastName= e.values[2];
var email= e.values[3];
var phone= e.values[4];
ContactsApp.createContact( timestamp , firstName , lastName , email , phone );
}
TypeError: Cannot read property "values" from undefined. (line 2
You will get that error message when attempting to run the function manually, as there is no event being passed to the function.
Disregard the remainder of the post (see Henrique's comment).
According to the documentation:
An event is passed to every event handler as the argument (e) . You
can add attributes to the (e) argument that further define how the
trigger works or that capture information about how the script was
triggered.
If you use the attributes, you must still set the trigger from the
Resources menu, as described in the sections above.
https://developers.google.com/apps-script/guide_events#TriggerAttributes
I can't explain why this is the case, but it seems you need to use the
simple event handler (ie naming the function onFormSubmit), as well as
applying an installable event handler to that same function. Then,
for me at least, it works as expected.
HTH, Adam
There's no "phone" argument in createContact function. Before attempting to create it from a more difficult to debug trigger, have you tried writing a simple function to create a simple contact just to check that you got it right?
Also, I remember an old error with on form submit parameters, that may be back. Anyway, it doesn't hurt to add a toString in all your parameters. e.g.
var email = e.values[3].toString(); //just to be sure