Question

So I am using a script i found on line to send an email everytime a Google Form is completed, which includes the data in the form via a Google Sheet.

function sendFormByEmail(e) 
{    
  var email = “email@address.com”; 

  var s = SpreadsheetApp.getActiveSheet();
  var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];    
  var message = "A new travel request has been submitted.";
  var subject = "New Travel Justification Request: ";

  for(var i in headers) 
    message += headers[i] + ': '+ e.namedValues[headers[i]].toString() + "\n\n";     

  subject += e.namedValues[headers[2]].toString() + " - starts " + e.namedValues[headers[15]].toString();

MailApp.sendEmail(email, subject, message); 

  // Based off of a script originally posted by Amit Agarwal - www.labnol.org
  // Credit to Henrique Abreu for fixing the sort order
}

It works well, but I want it to exclude the headers for empty cells, as not all parts of the form require completion.

I know very little, so all help is appreciated.

Was it helpful?

Solution

I have updated the original Google Form script to not include fields that are empty. You can add a simple condition in the for loop:

for(var i in headers) {
  if ( e.namedValues[headers[i]].toString() != "") {
     message += headers[i] + ': '+ e.namedValues[headers[i]].toString() + "\n\n";  
  }
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top