I'm getting an "undefined" error when I try to get the value from a cell in a spreadsheet. The thing is that if I execute the same command for a different cell I get the value in that cell. The only difference between those 2 cells is the way the value is produced.
The value in the cell that show correctly is produced directly from the Google Form associated with that spreadsheet. The value that doesn't show when called, is produced from a script I created in the Google Form.
Script for the Form (triggered on form submit):
// This code will set the edit form url as the value at cell "C2".
function assignEditUrls() {
var form = FormApp.getActiveForm();
var ss = SpreadsheetApp.openById("my-spreadsheet-id")
var sheet = ss.getSheets()[0];
var urlCol = 3; // column number where URL's should be populated; A = 1, B = 2 etc
var formResponses = form.getResponses();
for (var i = 0; i < formResponses.length; i++) {
var resultUrl = formResponses[i].getEditResponseUrl();
sheet.getRange(2 + i, urlCol).setValue(resultUrl);
}
SpreadsheetApp.flush();
}
Table (changed to HTML)
<table>
<tr> <!-- Row 1 -->
<td>Timestamp</td> <!-- A1 -->
<td>Name</td> <!-- B1 -->
<td>Edit form URL</td> <!-- C1 -->
</tr>
<tr> <!-- Row 2 -->
<td>5/26/2015 14:04:09</td> <!-- A2: this value came from the form submittion-->
<td>Jones, Donna</td> <!-- B2: this value came from the form submittion-->
<td>https://docs.google.com/forms/d/1-FeW-mXh_8g/viewform?edit2=2_ABaOh9</td> <!-- C2: this value came from the the script in the form -->
</tr>
</table>
Script in Spreadsheet (Triggered on form submit)
function onFormSubmit(e) {
// This script will get the values from different cells in the spreadsheet
// and will send them into an email.
var name = e.range.getValues()[0][1]; // This will get the value from cell "B2".
var editFormURL = e.range.getValues()[0][2]; // This will get the value from cell "C2".
var email = '[email protected]';
var subject = "Here goes the email subject."
var message = 'This is the body of the email and includes'
+ 'the value from cell "B2" <b>'
+ name + '</b>. This value is retrieved correctly.'
+ '<br>But the value from cell "C2" <b>'+ editFormURL
+ '</b> show as "undefined".';
MailApp.sendEmail(email, subject, message, {htmlBody: message});
}
The email looks like this:
Sented by: [email protected]
Subject: Here goes the email subject.
Body:
This is the body of the email and includes the value from cell "B2" Jones, Donna. This value is retrieved correctly.
But the value from cell "C2" undefined show as "undefined".
Question:
What am I doing wrong?
Logger.log()statements to your code in order to debug your code. Add aLogger.log('range is: ' + e.range.getA1Notation())statement at the top of youronFormSubmit(e)function. What is the range? Under the 'View' menu, choose the 'Logs' menu item, after submitting the form - Alan Wellse.range.getRow(). The column number is always the same for column X. - Alan Wells