简体   繁体   English

Google 表单根据复选框问题的答案发送电子邮件,允许多选项提交

[英]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.我有一个谷歌表单,问题的答案决定了谁在提交表单时收到 email。 The question is a multi-choice text box, so anywhere from 1 to 6 options can be chosen.该问题是一个多选文本框,因此可以选择 1 到 6 个选项。 The question is the variable var announce in the script and all the if statements are directed to check for cases of that answer.问题是脚本中的变量var announce ,所有if语句都用于检查该答案的情况。

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.如果选择了两个或更多选项,则不会发送 email。 There are no errors showing up in executions.执行中没有出现错误。

When multiple options are chosen, the data in the cell reads "Option 1, Option 4".选择多个选项时,单元格中的数据显示为“选项 1、选项 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?我在脚本中列出的所有案例仅适用于“选项 1”或“选项 4”等。有没有一种方法可以使这项工作无需对可能发生的 6 个选择的每个可能组合进行if语句?

   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.运行测试后的控制台日志:测试 1 - 在触发电子邮件的问题上选择的一个选项。 Email was sent. Email 已发送。 *Please note: I entered generic info in script to post - the logs show the actual values. *请注意:我在要发布的脚本中输入了通用信息 - 日志显示了实际值。 Example: option 6 is "Phone Call Home"示例:选项 6 是“Phone Call Home”

Oct 7, 2022, 2:32:21 PM Debug 2022 年 10 月 7 日下午 2:32:21 调试
{"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","",""]} {"authMode":"FULL","namedValues":{"Email Address":["heritagecougars@yanceync.net"],"Teacher Name":["J"],"如果适用,请在此表格中包含照片;您还可以为社交媒体创建图形并包括以下内容":[""],"选择公告渠道":["打电话回家"],"时间戳":["10/7/2022 14:32:21"] ,"您的消息/公告":["测试选择的一个选项"],"请包括时间范围":[""]},"范围":{"columnEnd":6,"columnStart":1,"rowEnd" :36,"rowStart":36},"source":{},"triggerUid":"12607666","values":["10/7/2022 14:32:21","heritagecougars@yanceync.net" "J","打电话回家","测试一个选项","",""]}

Test 2 - two options chosen on the question that triggers emails.测试 2 - 在触发电子邮件的问题上选择两个选项。 No emails sent.没有发送电子邮件。

Oct 7, 2022, 2:32:40 PM Debug 2022 年 10 月 7 日下午 2:32:40 调试
{"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","",""]} {"authMode":"FULL","namedValues":{"请包括时间范围":[""],"教师姓名":["J"],"您的消息/公告":["测试选择的 2 个选项"],"Email Address":["heritagecougars@yanceync.net"],"Timestamp":["10/7/2022 14:32:39"],"如果适用,在此表格中包含照片;您也可以创建社交媒体的图形,包括以下内容":[""],"选择公告渠道":["MHHS 网站,打电话回家"]},"范围":{"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: Google Forms 将检查的选项作为字符串发送:

"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.请确保您的 Google Forms 中相应问题中的选项不包含逗号。
  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" . announce.includes的参数应该是 Google Forms 中选项之一的值,即不是"Option 6"而应该是"Phone call home"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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