简体   繁体   English

如果/其他声明无法发送其他电子邮件

[英]If/Else Statement not working to send different emails

I am trying to write a script in google sheets that will send one of two different emails based on the response to a multiple choice question. 我正在尝试在Google表格中编写一个脚本,该脚本将根据对多项选择题的答复发送两种不同的电子邮件之一。 I can get my if/else statement to send either one or the other of the emails but it will not recognize the text of the multiple choice answer and send the correct email. 我可以通过if / else语句来发送一封或另一封电子邮件,但它无法识别多选答案的文本并发送正确的电子邮件。

Here is the full script: 这是完整的脚本:

function sendEmails() {
    var sheet = SpreadsheetApp.getActiveSheet();
    var startRow = 2;  // First row of data to process
    var numRows = 1;
    // Fetch the range of cells A2:B3
    var dataRange = sheet.getRange(startRow, 1, numRows, 8)
    // Fetch values for each row in the Range.
    var data = dataRange.getValues();
    for (var i = 0; i < data.length; ++i) {
        var row = data[i];
        var title = row[1];  // First column
        var startDate = row[3];       // Second column
        var endDate = row[4];
        var description = row[2];
        var location = row[6];
        var eventImport = row[5];
        var emailAddress = row[7];    
        var multchoice = row[8];

        if (multchoice == "Part Time") {
            var subject = "New Hire Part Time Email - " + startDate;
            var emailBody = "Congradulations"
            var htmlBody = "Congradulations! Part time person"
            MailApp.sendEmail (emailAddress, subject, emailBody);
        } else {   
            var subject = "New Hire Appointment - " + startDate;
            var emailBody = "Congratulations! We are excited"
            var htmlBody = "Congratulation! </i><br/>&nbsp;<br/> We are excited"
            MailApp.sendEmail(emailAddress, subject, emailBody);
        }
    }
}

I believe the problem is here: 我相信问题在这里:

 if (multchoice == "Part Time")

Any help is greatly appreciated! 任何帮助是极大的赞赏! I am a novice 我是新手

It looks like you are assigning your variables starting with '1' I stead of '0'. 看起来您正在分配从“ 1”开始而不是“ 0”的变量。 Start assigning them with 0 and counting up. 开始为它们分配0并开始计数。

Without an example sheet to use, I won't be able to do a whole lot of debugging for you. 如果没有示例表可供使用,我将无法为您进行大量调试。

However, Apps Script comes with it's own debugger. 但是,Apps Script带有它自己的调试器。 Select the function you wish you debug and click the Little bug icon beside the play button. 选择要调试的功能,然后单击播放按钮旁边的小错误图标。

Click on the sidebar where you want to set a breakpoint, where the code will stop executing. 单击要设置断点的侧边栏,代码将在该断点处停止执行。

在此处输入图片说明

Once it hits that breakpoint you can see all the variables currently within your scope. 一旦达到该断点,您就可以看到当前作用域内的所有变量。 So the array , value , and i variables are visible to you. 因此,您可以看到arrayvaluei变量。

在此处输入图片说明

Use this to your advantage and debug your code to find out where the issue is. 利用此功能可以发挥优势,并调试代码以找出问题所在。 Alternatively, you can use Logger.log() to log values at certain points within your code and then read back through the logs to try and determine where the problem lies. 另外,您可以使用Logger.log()在代码中的某些点记录值,然后重新阅读日志以尝试确定问题所在。

The problem is not with your if/else statement. 问题不在于您的if / else语句。 The problem is with how you are assigning your variables from your row[] array. 问题在于如何从row[]数组分配变量。 While you use regular numbers in the getRange() function, the range that is returned is an array of those cells. getRange()函数中使用常规数字时,返回的range是这些单元格的数组。 Arrays always start with an index of [0] . 数组始终以索引[0]开头。 Change var multchoice = row[8] to var multchoice = row[7] and your if/else statement will work (you'll want to change all of your other references, too). var multchoice = row[8]更改为var multchoice = row[7] ,您的if / else语句将起作用(您也想更改所有其他引用)。

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

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