使用else if语句时,Google脚本功能会中断。

时间:2016-05-27 13:34:23

标签: javascript

由于某些原因,在添加“else if”后我的功能崩溃了。当我还有其他的时候,它工作得很好。谁能告诉我我做错了什么。好像应该有效。我不是最好的,所以请保持友善:)

这是开场功能。

function SendGoogleForm(e) { 

var formData = e.values;  
var requestor = formData[1];
var newHire = formData[2];
var globalID = formData[3];
var startDate = Utilities.formatDate(new Date(formData[4]), Session.getScriptTimeZone(), "MMM d, yyyy");
var newhireRole = formData[5];
var newhireStatus = formData[6];

if (newhireRole == "Food Care") { 

var mailBody = HtmlService.createTemplateFromFile('foodCare');

mailBody.requestor = requestor;
mailBody.newHire = newHire;
mailBody.globalId = globalID;
mailBody.startDate = startDate;
mailBody.newHireRole = newhireRole;
mailBody.newHireStatus = newhireStatus;

var mailMessage = mailBody.evaluate().getContent();

var subject = "NACCC Food Care Pre-Approved On-boarding Request for " + newHire;

var recipient = 'juan.miranda@testr.com';

MailApp.sendEmail(recipient, subject, '',{htmlBody:mailMessage});

}else if (newhireRole == "Product Care"){

var mailBody = HtmlService.createTemplateFromFile('productCare');

mailBody.requestor = requestor;
mailBody.newHire = newHire;
mailBody.globalId = globalID;
mailBody.startDate = startDate;
mailBody.newHireRole = newhireRole;
mailBody.newHireStatus = newhireStatus;

var mailMessage = mailBody.evaluate().getContent();

var subject = "NACCC Product Care Pre-Approved On-boarding Request for " + newHire;

var recipient = 'juan.miranda@testr.com'; 

MailApp.sendEmail(recipient, subject, '',{htmlBody:mailMessage});

} else {

var mailBody = HtmlService.createTemplateFromFile('specialtyMaterials');

mailBody.requestor = requestor;
mailBody.newHire = newHire;
mailBody.globalId = globalID;
mailBody.startDate = startDate;
mailBody.newHireRole = newhireRole;
mailBody.newHireStatus = newhireStatus;

var mailMessage = mailBody.evaluate().getContent();

var subject = "NACCC Specialty Materials Pre-Approved On-boarding Request for " + newHire;

var recipient = 'juan.miranda@testr.com';

MailApp.sendEmail(recipient, subject, '',{htmlBody:mailMessage});

}

}

最后的花括号是红色的,我不明白为什么。我试着评论“if”语句的不同部分但由于某种原因它不会让我做三个条件。有任何想法吗? :/

1 个答案:

答案 0 :(得分:1)

问题似乎与变量声明有关,您使用var并重新声明等等。

看起来是时候重构了,你做了很多相同的事情 - 所以让我们对它进行参数化,以便更容易阅读和调试(并遵循{{3 }})。

你有一个非常常见的步骤来构建一个电子邮件,让它成为一个功能:

function constructEmail(file, requestor, newHire, globalId, startDate, newHireRole, newHireStatus) {
    let mailBody = HtmlService.createTemplateFromFile(file);

    body.requestor = requestor;
    body.newHire = newHire;
    body.globalId = globalId;
    body.startDate = startDate;
    body.newHireRole = newHireRole;
    body.newHireStatus = newHireStatus;

    return body.evaluate().getContent();
}

然后我们可以调用它并将内容作为回报。

function SendGoogleForm(e) {

  let formData = e.values;
  let requestor = formData[1];
  let newHire = formData[2];
  let globalId = formData[3];
  let startDate = Utilities.formatDate(new Date(formData[4]), Session.getScriptTimeZone(), "MMM d, yyyy");
  let newHireRole = formData[5];
  let newHireStatus = formData[6];

  let mailMessage = null;
  let subject = "NACCC Food Care Pre-Approved On-boarding Request for " + newHire
  let recipient = 'juan.miranda@testr.com';

  switch (newHireRole) {
    case "Food Care":    
    mailMessage = 
        constructEmail('foodCare',
                       mailBody,
                       requestor,
                       newHire,
                       globalId,
                       startDate,
                       newHireRole,
                       newHireStatus);    
    break;

    case "Product Care":    
    mailMessage = 
        constructEmail('specialtyMaterials',
                       mailBody,
                       requestor,
                       newHire,
                       globalId,
                       startDate,
                       newHireRole,
                       newHireStatus);    
    break;

    default:    
    mailMessage = 
        constructEmail('productCare',
                       mailBody,
                       requestor,
                       newHire,
                       globalId,
                       startDate,
                       newHireRole,
                       newHireStatus);
    break;
  }

  MailApp.sendEmail(recipient, subject, '', {
      htmlBody: mailMessage
  });

}

首先,让我们习惯使用DRY关键字来限制变量的范围。接下来,我们将创建一个辅助方法来执行重复性工作,然后我们将使用let操作而不是if / else if / else语法。这应该使它更容易阅读,遵循和调试。