'Send email notification with response data only upon google form submission

sample dataset Image

`function onFormSubmit(){

var ss = SpreadsheetApp.getActive().getSheetByName("Form responses 4");
var lastresponse =       ss.getRange(ss.getLastRow(), 1, 1, 18).getValues();
var customerEmail = lastresponse[0][10];  
var Subject = "sample updates # " + lastresponse[0][1];
var alias = GmailApp.getAliases();


var Message = "Hello Team" +  ",<br/>"
+ "Please Find Below Details :<br/>"
+ "BN : <b>" + lastresponse[0][1] + ".  </b><br/>"
+ "PN : <b>" + lastresponse[0][12] + "</b><br/>"
+ "MN : <b>" + lastresponse[0][13] + "</b><br/>"
+ "MN1 : <b>" + lastresponse[0][14] + "</b><br/>"
+ "QP = <b>" + lastresponse[0][3] + "</b><br/>"
+ "QS = <b>" + lastresponse[0][4] + "</b><br/>"
+ "FR = <b>" + lastresponse[0][5] + "</b><br/>"
+ "SLR = <b>" + lastresponse[0][6] + "</b><br/>"
+ "SLRV = <b>" + lastresponse[0][7] + "</b><br/>"
+ "FR = <b>" + lastresponse[0][8] + "</b><br/>"
+ "PR = <b>" + lastresponse[0][9] +"</b><br/>"
+ "Co : <b>" + lastresponse[0][11] + "</b><br/>"

+ "<b>please do not, reply on this    email.<b><br/>"
+ "Thank you";

GmailApp.sendEmail(customerEmail, Subject, Message, { from:alias[0],htmlBody: Message,name: 'Updates'});
 } `

Hope you guys are doing great. I have a question which is generic but somehow I am new to google app script so unable to find it. I have a google form with 10 questions, when user submits form I receive email notification as expected but I receive all 10 questions even some of the questions unanswered by user. Is there anything I am missing that I receive data of only answered questions in email notification.

E.g if user respond to 5 questions out of 10 then I receive only those 5 questions details in email notification.

Any help on this is appreciated.

If already provided solutions then you can just tag that solution here also. Thank you.



Solution 1:[1]

You can save this:

function onFormSubmit() {
  var ss = SpreadsheetApp.getActive().getSheetByName("Form responses 4");
  var lastresponse = ss.getRange(ss.getLastRow(), 1, 1, 18).getValues();
  var customerEmail = lastresponse[0][10];
  var Subject = "sample updates # " + lastresponse[0][1];
  var alias = GmailApp.getAliases();
  var Message = "Hello Team" + ",<br/>" + "Please Find Below Details :<br/>" + "BN : <b>" + lastresponse[0][1] + ".  </b><br/>" + "PN : <b>" + lastresponse[0][12] + "</b><br/>" + "MN : <b>" + lastresponse[0][13] + "</b><br/>" + "MN1 : <b>" + lastresponse[0][14] + "</b><br/>" + "QP = <b>" + lastresponse[0][3] + "</b><br/>" + "QS = <b>" + lastresponse[0][4] + "</b><br/>" + "FR = <b>" + lastresponse[0][5] + "</b><br/>" + "SLR = <b>" + lastresponse[0][6] + "</b><br/>" + "SLRV = <b>" + lastresponse[0][7] + "</b><br/>" + "FR = <b>" + lastresponse[0][8] + "</b><br/>" + "PR = <b>" + lastresponse[0][9] + "</b><br/>" + "Co : <b>" + lastresponse[0][11] + "</b><br/>" + "<b>please do not, reply on this    email.<b><br/>" + "Thank you";
  GmailApp.sendEmail(customerEmail, Subject, Message, { from: alias[0], htmlBody: Message, name: 'Updates' });
}

Using the Event Object on a form submission

function onFormSubmit(e) {
  const ss = SpreadsheetApp.getActive();
  var sh = ss.getSheetByName("Form responses 4");
  var vs = [e.values()];
  var customerEmail = vs[0][10];
  var Subject = "sample updates # " + vs[0][1];
  var alias = GmailApp.getAliases();
  var Message = "Hello Team" + ",<br/>" 
    + "Please Find Below Details :<br/>"
    + "BN : <b>" + vs[0][1] + ".  </b><br/>"
    + "PN : <b>" + vs[0][12] + "</b><br/>"
    + "MN : <b>" + vs[0][13] + "</b><br/>"
    + "MN1 : <b>" + vs[0][14] + "</b><br/>"
    + "QP = <b>" + vs[0][3] + "</b><br/>"
    + "QS = <b>" + vs[0][4] + "</b><br/>"
    + "FR = <b>" + vs[0][5] + "</b><br/>"
    + "SLR = <b>" + vs[0][6] + "</b><br/>"
    + "SLRV = <b>" + vs[0][7] + "</b><br/>"
    + "FR = <b>" + vs[0][8] + "</b><br/>"
    + "PR = <b>" + vs[0][9] + "</b><br/>"
    + "Co : <b>" + vs[0][11] + "</b><br/>"
    + "<b>please do not, reply on this    email.<b><br/>"
    + "Thank you";
  GmailApp.sendEmail(customerEmail, Subject, Message, { from: alias[0], htmlBody: Message, name: 'Updates' });
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1