If 语句允许单个和多个 if 选项

时间:2021-06-22 12:55:44

标签: if-statement google-apps-script

我有一个 If 语句,它依赖于在 google 表单中所做的选择。目前一切正常,但是,问题选择允许他们选择所有适用的选项。如果选择了多个选项,脚本将失败。

目前的工作方式 - 人员填写表单,脚本获取信息并输入到文档中,根据问题的选择给出指定的消息(下面脚本中的 cif 和消息),然后通过电子邮件发送正确的收件人(在表单中选择)。如果 cif 只提交一个选项,它的效果会很好。

我应该向脚本添加/编辑什么以允许多个选择,从而允许向文档输入多个消息,而不必为每个场景编写 If 状态?

// Global variables 
var docTemplate = "1EoBcz0BK4R5hm-q5pR68xnQnR8DlR56XzjxRrgsu4uE";  // *** replace with your template ID ***
var docName     = "You got a High 5";

function onFormSubmit(e) { // add an onsubmit trigger
  
// Values come from the spreadsheet form  
   var observer = e.values[1]
   var teacher = e.values[2]
   var email = e.values[2]    
   var period = e.values[4] 
   var time = e.values[3]      
   var cif = e.values[5]    
   var comments = e.values[6]
   var message;
   if (cif == 'READ - Variety of texts') {
  message = 'read quote'
}
else if (cif == 'WRITE - Paper or electronic') {
  message = 'write quote'
}
else if (cif == 'THINK - Actively engaged students') {
  message = 'think quote'
}    
else if (cif == 'TALK - Purposeful discussion') {
  message = 'talk quote'
}        
else if (cif == 'MOVE - Students moving through class') {
  message = 'move quote'
}   
else if (cif == 'CIF not observed') {
  message = 'CIF not observed'
}  


// Get document template, copy it as a new temp doc, and save the Doc’s id
   var copyId = DriveApp.getFileById(docTemplate)
                .makeCopy(docName+' for '+teacher)
                .getId();
  
// Open the temporary document
   var copyDoc = DocumentApp.openById(copyId);
  
// Get the document’s body section
   var copyBody = copyDoc.getActiveSection();
  
// Replace place holder keys,  
  copyBody.replaceText('keyobserver', observer);
  copyBody.replaceText('keyperiod', period);
  copyBody.replaceText('keytime', time);
  copyBody.replaceText('keycif', cif);
  copyBody.replaceText('keycomments', comments);
  copyBody.replaceText('keymessage', message)


  
   var todaysDate = Utilities.formatDate(new Date(), "GMT", "MM/dd/yyyy"); 
   copyBody.replaceText('keyTodaysDate', todaysDate);
  
// Save and close the temporary document
   copyDoc.saveAndClose();
  
// Convert temporary document to PDF by using the getAs blob conversion
   var pdf = DriveApp.getFileById(copyId).getAs("application/pdf"); 
  
// Attach PDF and send the email
   var subject = "High 5 - it matters.";
   var body    = "You got a High 5! See attached PDF. " +
                 "Please do not reply to this email.  You will be asked to supply a response thorugh a link within the attached PDF.  " +
                 "'Do all the good you can. By all the means you can. In all the ways you can. In all the places you can. At all the times you can. To all the people you can. As long as you ever can. -John Wesley'";
 
  MailApp.sendEmail(email, subject, body, {htmlBody: body, attachments: pdf});
  // Delete temp file
   DriveApp.getFileById(copyId).setTrashed(true);
}

1 个答案:

答案 0 :(得分:0)

有多个选择的答案将返回一个逗号分隔的字符串形式的值。

例如:

e.values[5] 可以有:

'READ - Variety of texts, WRITE - Paper or electronic, THINK - Actively engaged students, TALK - Purposeful discussion, MOVE - Students moving through class, CIF not observed' 

在单个字符串中使用 == 将不起作用,因为它与您要比较的字符串具有不同的内容。这会导致您的所有 if 语句失败,因为它们都不与字符串匹配。这会导致未定义的变量 message 导致 replaceText() 函数出错。

要解决此问题,请替换以下代码行:

var message;
   if (cif == 'READ - Variety of texts') {
  message = 'read quote'
}
else if (cif == 'WRITE - Paper or electronic') {
  message = 'write quote'
}
else if (cif == 'THINK - Actively engaged students') {
  message = 'think quote'
}    
else if (cif == 'TALK - Purposeful discussion') {
  message = 'talk quote'
}        
else if (cif == 'MOVE - Students moving through class') {
  message = 'move quote'
}   
else if (cif == 'CIF not observed') {
  message = 'CIF not observed'
}  

与:

  var message = [];
  
  if (cif.match('READ - Variety of texts')) {
    message.push('read quote');
  }
  
  if (cif.match('WRITE - Paper or electronic')) {
    message.push('write quote');
  }
  
  if (cif.match('THINK - Actively engaged students')) {
    message.push('think quote');
  }    
  
  if (cif.match('TALK - Purposeful discussion')) {
    message.push('talk quote');
  }        
  
  if (cif.match('MOVE - Students moving through class')) {
    message.push('move quote');
  }   
  
  if (cif.match('CIF not observed')) {
    message.push('CIF not observed')
  }

并在您的

copyBody.replaceText('keymessage', message);

添加join函数,当内容大于1时,使消息以逗号分隔:

copyBody.replaceText('keymessage', message.join(',');

示例用法:

多个 CIF:

var cif = 'READ - Variety of texts, WRITE - Paper or electronic, THINK - Actively engaged students, TALK - Purposeful discussion, MOVE - Students moving through class, CIF not observed';

输出: enter image description here

单一到岸价:

var cif = 'WRITE - Paper or electronic';

输出: enter image description here

参考文献