Google-sheets – How to write a script/formula for Google Form Responses to send submitted form to different emails based on a response

google sheetsgoogle-apps-scriptgoogle-forms

I have a registration form that we are using for my company. We have 6 locations. I want to have the registration form that is filled out on Google forms to go to specific emails based on their answer for what location they are at. I have tried a few scripts, and have had no luck.

Best Answer

You'd write an if then statement that covers your 6 locations. It'd be something like this . . . though this isn't exact.

function sendEmails() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.getActiveSheet();

  var lastRow = getLastRow();
  var range = sheet.getRange('C'. lastRow);//location cell
  var location = range.getValue();
  if (location === 'heaven'){
     emailAddress = 'heaven@location.com'
     } 
    elseif (location === 'hell'){
     emailAddress = 'hades@location.com'
   } elseif (location === 'purgatory') {
     emailAddress = 'purg@location.com' 
   }else { 
     emailAddress ='void@location.com'
   }


MailApp.sendEmail({
     to: emailAddress,
     subject: subject,
     htmlBody: message     
   });