match() is not working with Number field

2019-02-20 04:00发布

I'm trying to perform an action on rows that have specific words in email column by filtering it using the regular expression.

enter image description here

Problem is that match() produces the following error

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

My 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条回答
Anthone
2楼-- · 2019-02-20 04:21

match() has to receive a string, cell C4 produces a Number, hence the error.

Change this line,

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

Into the following should fix the TypeError

Notice the .toString() method ahead of .match()

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

查看更多
登录 后发表回答