Google Form Submission Values are Undefined - google-apps-script

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)

Related

Call custom function in google sheets only once and save value to cell

I have written a custom google apps script function that is being called by a google sheet. In this function an API is called and the result of this API is being returned so that it gets displayed in the calling cell of the google sheet.
My problem now is that the sheet calls this function everytime I open the document and there are over 80.000 cells with this function.
Is it possible to save the returned value to the cell and don't call the custom function again when an value has been returned? I want the function being called only once per cell, even if I close the document and reopen it. The returned value should be saved until something else is being written into to cell. That would make my sheets document much more usable than the current state.
From the question
Is it possible to save the returned value to the cell and don't call the custom function again when an value has been returned? I want the function being called only once per cell, even if I close the document and reopen it. The returned value should be saved until something else is being written into to cell. That would make my sheets document much more usable than the current state.
By solely using the custom function it's not possible. There isn't a straight forward solution is to achieve this, in order to keep things simple you should look for another way to implement what is being done by the custom funtion.
One option is to use a trigger (including a custom menu / a function assined to a drawing) to check if the cell that should have the value returned by the custom function is empty and in such case fill it with the corresponding value. The best trigger to use will depend on your spreadsheet workflow.
As specified by Ruben this is not possible, with a custom function.
In my particular case I have resorted to using an Apps Script function that is triggered by an edit event of the spreadsheet and verifies if the event is in the column where the function that I want to execute only once should be, later replacement its content with the result of calling the API.
function freezeValue(e) {
var rangeEvent = e.range;
var col = rangeEvent.getColumnIndex();
if (col === 2) { #Verify column of event
var value = rangeEvent.getValue();
/*Call your api*/
result_api = CALL_API(value)
rangeEvent.setValue(result_api);
}
}
Keep in mind that this implementation only works when each of the cells is edited one by one. To do the same with a row or a complete array of elements, you must go through each of the cells and follow the same procedure.

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

onFormSubmit(e) returning NULL e value

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.

Call a function with an argument in Google app's newTrigger("myFunction")

I am currently trying to call a function with an argument in Google app's newTrigger() function.
I have a function called writeToSheet(sheet). Where as I enter the sheet name into the argument and the custom output is written to said sheet. It all works fine, but I would like to pass this into a time driven trigger with different sheets being written to at different times.
So far, entering ScriptApp.newTrigger('writeToSheet("Production Sheet")') within Google's trigger building example doesn't work. How would you go about solving this problem?
So far, I have had to create a function that executes what I need.
function prodSheetUpdate() {
writeToSheets("Production Sheet");
}
Then pass that function in my trigger.
ScriptApp.newTrigger("ProdSheetUpdate")
...
...
It doesn't seem like you can pass an argument in a trigger. If anybody knows otherwise, I am still curious!
you must pass a string to newTrigger
ScriptApp.newTrigger('writeToSheet('+'Production Sheet'+')')
or
var mystring
ScriptApp.newTrigger('writeToSheet('+mystring+')')

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