You are make a few confusions about variables. field
is an integer that you use as an index so a condition like if (field == 'Email')
can never be true.
You have to get the key values from the form response object and use that to select values.
The other answer shows a possible way to achieve that by reading the spreadsheet but there is a more simple and direct way to do it directly from the object.
for(var k in e.namedValues){ keys.push(k) };
Logger.log("total " + keys.length + " keys: " + keys);
Then you can use that to check every response element. Returned values from text questions are string so you don't need to use toString()
.
The full code is shown below:
function formSubmit(e){
var keys = [];
var email = '';
var subject = '';
var message = '';
var myemail = Session.getActiveUser().getEmail();
for(var k in e.namedValues){
keys.push(k);
}
Logger.log("total " + keys.length + " keys: " + keys);
for(var field in keys) {
message += field + ' :: '+ e.namedValues[keys[field]] + "\n\n";
if (keys[field] == 'Subject') {
subject += e.namedValues[keys[field]];
}
if (keys[field] == 'Email') {
email = e.namedValues[keys[field]];
}
}
MailApp.sendEmail(email, subject, message, {replyTo:myemail});
}
btw, you don't need to hardcode your email address since GS has a method for it, the onFormSubmit
trigger being an installable trigger it runs as you and you can use Session.getActiveUser().getEmail()
.