I want to notify mentors if they're selected by a user on a Google Form.
The names of the mentors are in a drop-down menu in the Google Form. This is the form: https://forms.gle/iNuA5nB2J9koKhXt6. I'm storing responses in a spreadsheet, I've created a different worksheet in the same spreadsheet named "namenemail" in which I've stored the names and email IDs of the mentors. This is the spreadsheet: https://docs.google.com/spreadsheets/d/15xS_-I6Jw3cqx-00Ink4FFTcfmI3Zi-hBGlMU2x0Cb4/edit?usp=sharing.
I want to send an email to the mentor whose name is selected from the dropdown menu with all the answers from the response of google form submission on submit. I can't seem to get it right.
I found this one script which only works when the question with the dropdown menu is the first question which is not the case in my form. I tried modifying it but I can't seem to get it right. The code also flags an error
TypeError: Cannot read property 'range' of undefined (line 6, file "Code").
Here is the script that I'm using:
function onSubmit(e) {
//setup the spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
//get the range from OnFormSubmit
var range = e.range;
Logger.log("DEBUG: the range is "+range.getA1Notation());//DEBUG
// get the data for the range
var response = range.getValues();
// get the clinician name from the form submission
var mentor = response[0][2];
Logger.log("DEBUG: Mentor name = "+Name);// DEBUG
// get the emails list
var emailSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("namenemail");
// get ALL the data from this sheet
var emaildata = emailSheet.getDataRange().getValues();
// check how many rows of data
var emailLastRow = emailSheet.getLastRow();
// start the loop through the emails data
for (var i=1; i<emailLastRow; i++){
// if the mentor is equal to Email
if (mentor == emaildata[i][0]){
// there is a match
//Next, get the email address
var emailmentor = emaildata[i][1];
Logger.log("DEBUG: clinician = "+emaildata[i][0]+", email address: "+emailmentor);// DEBUG
// Finally, send the Email.
var theirName = e.values[0];
var theirEmail = e.values[1];
var theEnquiry = e.values[4];
var subject = "New Form Submitted";
var message = "New Enquiry by: \n\n EMAIL: " + theirEmail + " \n Name: " + theirName + " \n\n Regarding Enquiry: \n" + theEnquiry;
MailApp.sendEmail(emailClinician, subject, message);
}
}
}