1

I want to write a program for a friend, who is working in a Human Resources department, that automatically sends letters of refusals to rejected applicants.

To have an overview of the application, he uses a spreadsheet where names, position, interviews, etc. are recorded. Every applicant has one row in the document.

If he rejects a candidate, he puts an "A" in a specific column. I want the program to recognize those "A"s, write an email to the applicant and then put the word "done" in the cell next to the "A".

The code is the following:

function Absagen() {
    var sheet = SpreadsheetApp.getActiveSheet();
    var range = sheet.getRange(1, 5);
    var endRow = range.getValue();
    var startRow = 1;
    var numRows = endRow;
    var dataRange = sheet.getRange(startRow, 1, numRows, 10)
    var data = dataRange.getValues();

    for (i in data) {
        var row = data[i];
        var emailAddress = row[0];
        var absage = row[8];
        var absageSent = row[9];
        var mailLanguage = row[3];
        var gender = row[7];
        var firstName = row[5];
        var lastName = row[6];
        Logger.log("absage = "+ absage +" ?");
        if (absage == "A" && absageSent != "DONE" && mailLanguage == "2" && gender == "1"){
            var message = "<HTML><BODY>"
                              +"blablabla";
            var subject = "Ihre Bewerbung";
            MailApp.sendEmail(emailAddress, subject, "", {htmlBody: message});
            var destRow = Number(startRow); ++ destRow;
                sheet.getRange((destRow+i), 10).setValue("DONE");               
            SpreadsheetApp.flush();
        }
    }
}
4

2 回答 2

1

最初的问题,即“DONE”值被写入多个位置,然后写入错误的行,可以通过简化代码来解决。可以通过相同的简化来避免与数据类型强制相关的问题(来自 Serge 的答案的评论中的字符串与数字的东西)。

这是看起来令人困惑的第一件事。您似乎期望单元格E1包含一个数字,该数字将告诉您要评估多少行。

var range = sheet.getRange(1, 5);
var endRow = range.getValue();

然后你设置startRow=1,并用它来读取你的数据。时i===0,您再次设置firstName为单元格的内容。E1有什么不对劲。要么你会得到不是数字的东西endRow,要么你会尝试处理你的标题。对于这个特定的功能,这可能没问题,但最好使用可以可靠重复的模式。

让我们假设第一行包含电子表格的标题,并且所有后续行都包含需要处理的相关数据。如果这些值的数字被强制输入为字符串,或者这些列的格式设置为“纯文本”,则唯一可能留下的数据类型强制是在比较中mailLanguagegender

function Absagen() {
    var sheet = SpreadsheetApp.getActiveSheet();
    var headerRows = 1; // There are this many rows of header info
    var dataRange = sheet.getDataRange();  // Range from A1 to last row & col with data
    var data = dataRange.getValues();  // 2-dimensional array from datarange

    // Start after header rows, process all remaining data rows
    for (var rowNum=headerRows; rowNum < data.length; rowNum++) {
        var row = data[rowNum];
        var emailAddress = row[0];
        var absage = row[8];
        var absageSent = row[9];
        var mailLanguage = row[3];
        var gender = row[7];
        var firstName = row[5];
        var lastName = row[6];
        Logger.log("absage = "+ absage +" ?");
        if (absage == "A" && absageSent != "DONE" && mailLanguage == 2 && gender == 1){
            var message = "<HTML><BODY>"
                              +"blablabla";
            var subject = "Ihre Bewerbung";
            MailApp.sendEmail(emailAddress, subject, "", {htmlBody: message});
            // Range for update is at (rowNum+1) because the _array_ starts at 0,
            // but sheet rows start at 1.
            sheet.getRange((rowNum+1), 10).setValue("DONE");               
            SpreadsheetApp.flush();
        }
    }
}
于 2013-07-25T17:16:42.380 回答
0

如果您使用记录器查看中间值(请参见下面代码中的示例)并且如果您稍微更改一下您的条件(即使我不知道什么应该是“blabla nothing”,那么这个脚本应该不会太难调试重要的” ;-)

Logger.log('absage = '+absage+' ?');
if (blaba, nothing important && absage == "A" && absageSent!='DONE'){// prevent sending multiple time and send only if "A" and if 'nothing important !
    var message = "<HTML><BODY>"
                      +"blablabla";
    var subject = "Ihre Bewerbung bei Westwing Home & Living";
    MailApp.sendEmail(emailAddress, subject, "", {htmlBody: message});
    var destRow = Number(startrow); ++ destRow;
    sheet.getRange((destRow+i), 10).setValue("DONE");
    SpreadsheetApp.flush();
}

在这 looooong 系列评论之后进行编辑。 正如我所说,使用记录器可以帮助您了解发生了什么。我终于把你的代码复制到一个 SS 中进行测试,不到一分钟就看到了问题。这是修改后的代码和随之而来的记录器。仔细看看,你就会明白发生了什么。

...
var destRow = Number(startRow)+Number(i);
  Logger.log(startRow+' '+i+' '+(startRow+i))
  Logger.log(startRow+' '+i+' '+(Number(startRow)+Number(i)))
sheet.getRange(Number(destRow), 10).setValue("DONE");
SpreadsheetApp.flush();
...

在此处输入图像描述

于 2013-07-24T21:58:00.897 回答