match()不适用于“数字”字段

时间:2018-07-12 22:16:20

标签: google-apps-script google-sheets

我试图通过使用正则表达式对其进行过滤,对电子邮件列中具有特定单词的行执行操作。

enter image description here

问题是match()产生以下错误

TypeError: Cannot find function match in object 1234. (line 25, file "Code")

我的代码

    function smartDelete() {
    // smartDelete settings goes here, 
    var badDomains = ["bad\\.com", "red\\.com"];

    var emailColumnNumber = 2;

    var regExpModifiers = "i";

    // Gain access data in the sheet
    var sheet = SpreadsheetApp.getActiveSheet();
    var rows = sheet.getDataRange();
    var numRows = rows.getNumRows();
    var values = rows.getValues();
    var rowsDeleted = 0;
    var Action = false;

    // delete procedure
    for (var i = 0; i <= numRows - 1; i++) {
        var row = values[i];
        Action = false;

        // check bad domains
        for (var j = 0; j <= badDomains.length - 1; j++) {
            var myPattern = new RegExp(badDomains[j], regExpModifiers);
            var status = row[emailColumnNumber].match(myPattern);
            if (status) {
                // match found, mark this row for delete
                Action = true;
                break;
            };
        };

        // execute delete.
        if (Action) {
            sheet.deleteRow((parseInt(i) + 1) - rowsDeleted);
            rowsDeleted++;
        };
    };
}

1 个答案:

答案 0 :(得分:2)

match()必须接收一个字符串,单元格C4会产生一个Number,因此会出错。

更改此行

var status = row[emailColumnNumber].match(myPattern);

以下内容应修复TypeError

  

.toString()之前注意.match()方法

var status = row[emailColumnNumber].toString().match(myPattern);