简体   繁体   中英

Google Form send emails based on answer of checkbox question which allows for multi-option submission

I have a google form where the answer to a question determines who receives an email upon form submission. The question is a multi-choice text box, so anywhere from 1 to 6 options can be chosen. The question is the variable var announce in the script and all the if statements are directed to check for cases of that answer.

My current script works when one option only is chosen (working script = the correct department receives an email). If two or more options are chosen, no email is sent. There are no errors showing up in executions.

When multiple options are chosen, the data in the cell reads "Option 1, Option 4". All my listed cases in the script are for only "Option 1" or "Option 4", etc. Is there a way to make this work without having to make an if statement for every possible combination of the 6 choices that could potentially happen?

   function sendFormByEmail(e)
{      
  // Remember to replace XYZ with your own email address  
  var named_values = e.namedValues  
  var teachername = named_values["Teacher Name"];    
  var info = named_values["Your message/announcement"];  
  var time = named_values["Please include time frame"];  
  var photos = named_values["Include photos with this form if applicable; you can also create the graphic for social media and include below"];  
  var announce = named_values["Choose announcement outlet"];
  var email;
 
 // Optional but change the following variable  
// to have a custom subject for Google Docs emails  
  
// The variable e holds all the form values in an array.  
// Loop through the array and append values to the body.  
var message = "";      
for(var field in e.namedValues) {    
  message += field + ' :: ' 
                + e.namedValues[field].toString() + "\n\n"; 
   }   
    
  if(announce.includes("Option 1")){
    var email = "dept1@school.net";
    var subject = "Option 1 request";
    MailApp.sendEmail(email, subject, message);
   // put here the case clause, don't include break;
  }
  if(announce.includes("Option 2")){     
    var email = "dept2@school.net";
    var subject = "Option 2 request"; 
     MailApp.sendEmail(email, subject, message);      
  }   
  if(announce.includes("option 3")) {    
    var email = "dept3@school.net";
    var subject = "Option 3 request";  
     MailApp.sendEmail(email, subject, message);      
  }
  if(announce.includes("Option 4")) {    
    var email = "dept4@school.net";  
    var subject = "Option 4 request";  
     MailApp.sendEmail(email, subject, message);     
  }
  if(announce.includes("Option 5")){
    var email = "dept5@school.net";
    var subject = "Option 5 request";
     MailApp.sendEmail(email, subject, message);   
  }
  if(announce.includes("Option 6")){
    var email = "dept6@school.net";
    var subject = "Option 6 Request";   
     MailApp.sendEmail(email, subject, message);   
   }
  
    }

Console Logs after running tests: Test 1 - one option chosen on the question that triggers the emails. Email was sent. *Please note: I entered generic info in script to post - the logs show the actual values. Example: option 6 is "Phone Call Home"

Oct 7, 2022, 2:32:21 PM Debug
{"authMode":"FULL","namedValues":{"Email Address":["heritagecougars@yanceync.net"],"Teacher Name":["J"],"Include photos with this form if applicable; you can also create the graphic for social media and include below":[""],"Choose announcement outlet":["Phone call home"],"Timestamp":["10/7/2022 14:32:21"],"Your message/announcement":["test one option chosen"],"Please include time frame ":[""]},"range":{"columnEnd":6,"columnStart":1,"rowEnd":36,"rowStart":36},"source":{},"triggerUid":"12607666","values":["10/7/2022 14:32:21","heritagecougars@yanceync.net","J","Phone call home","test one option chosen","",""]}

Test 2 - two options chosen on the question that triggers emails. No emails sent.

Oct 7, 2022, 2:32:40 PM Debug
{"authMode":"FULL","namedValues":{"Please include time frame ":[""],"Teacher Name":["J"],"Your message/announcement":["test 2 options chosen"],"Email Address":["heritagecougars@yanceync.net"],"Timestamp":["10/7/2022 14:32:39"],"Include photos with this form if applicable; you can also create the graphic for social media and include below":[""],"Choose announcement outlet":["MHHS Website, Phone call home"]},"range":{"columnEnd":6,"columnStart":1,"rowEnd":37,"rowStart":37},"source":{},"triggerUid":"12607666","values":["10/7/2022 14:32:39","heritagecougars@yanceync.net","J","MHHS Website, Phone call home","test 2 options chosen","",""]}

Google Forms is sending the options checked as a string:

"Choose announcement outlet":["MHHS Website, Phone call home"]

Considering this, replace

var announce = named_values["Choose announcement outlet"];

by

var announce = named_values["Choose announcement outlet"][0].split(',');

Notes:

  1. Assure that the options in corresponding question in your Google Forms doesn't include commas.
  2. The parameters of announce.includes should be the value of one of the options in the Google Forms, ie instead of "Option 6" it should be "Phone call home" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM