2015-04-21 67 views
0

我想显示在Browser.Msg框中找到重复的工作表并通过电子邮件发送重复的字符串。检查重复的脚本

此外,额外的列可以写入状态为“DUPLICATE - YES”的那一行。然而,只要通过电子邮件/弹出就可以了。

我试过记录数据。我试过设置变量。

function checkDuplicates() { 
 
    var sheet = SpreadsheetApp.getActiveSheet(); 
 
    var dataRange = sheet.getRange("DATA!F2:F"); // Set Any Range 
 
    // "A:A" is for Column A 
 
    // And if you want to check duplicates for whole sheet then try this: 
 
    // var dataRange = sheet.getDataRange(); 
 
    var data = dataRange.getValues(); 
 
    var numRows = data.length; 
 
    var numColumns = data[0].length; 
 
    var dupes = false; 
 
    var okdupes0 = 0; 
 
    var nodupes0 = 0; 
 
    var totbookings0 = 0; 
 
    
 
    var formats = []; 
 
    var values = []; 
 
    for (var i = 0; i < numRows; i++) { 
 
    formats[i] = []; 
 
    for (var j = 0; j < numColumns; j++) { 
 
     formats[i][j] = 'WHITE'; 
 
     if (data[i][j] != '') { 
 
     values.push([data[i][j], i, j]); 
 
     } 
 
    } 
 
    } 
 
    var numValues = values.length; 
 
    
 
    for (var k = 0 ; k < numValues - 1; k++) { 
 
    if (formats[values[k][1]][values[k][2]] == 'WHITE') { 
 
     for (var l = k + 1; l < numValues; l++) { 
 
     if (values[k][0] == values[l][0]) { 
 
      formats[values[k][1]][values[k][2]] = 'RED'; 
 
      formats[values[l][1]][values[l][2]] = 'RED'; 
 
      var dupes = true; 
 
     } 
 
     } 
 
      var okdupes = okdupes0++; 
 
    } 
 
      var totbookings = totbookings0++; 
 
    } 
 
    
 
    if (dupes) { 
 
// var okdupes = okdupes -1; 
 
    var nodupes = totbookings - okdupes; 
 
    var emailAddress = "[email protected]"; // First column 
 
    var message = + nodupes + " Duplicate voucher(s) has been found from the system. Duplicate vouchers has been marked with red color.";  // Second column 
 
    var subject = "System: " + nodupes + " Duplicate Voucher(s) Found!"; 
 
    MailApp.sendEmail(emailAddress, subject, message); 
 
    Browser.msgBox('Warning!', ''+ nodupes +' Possible duplicate voucher(s) has been found and colored red! Please contact the rep who has made the sale. '+ totbookings +' bookings has been scanned through for duplicates.', Browser.Buttons.OK); 
 
    } else { 
 
    Browser.msgBox('All good!', 'No duplicate vouchers found.', Browser.Buttons.OK); 
 
    } 
 
    
 
    dataRange.setBackgroundColors(formats); 
 
}

回答

1

你可能值的数组转换为字符串,然后使用match数出现。

此代码可用于查找重复项,即使是来自二维数组。它不能确定重复来自哪个单元格。所有重复项的值都放入一个数组中。

function findDups() { 
    var testArray = [['one','two','three'],['three','four','five']]; 
    var allDataAsString = testArray.toString(); 
    Logger.log('allDataAsString: ' + allDataAsString); 

    //Create one Dimensional array of all values 
    var allDataInArray = allDataAsString.split(","); 
    var pattern; 
    var arrayOfDups = []; 

    for (var i = 0;i<allDataInArray.length;i++) { 
    var tempStr = allDataInArray[i]; 

    // the g in the regular expression says to search the whole string 
    // rather than just find the first occurrence 
    var regExp = new RegExp(tempStr, "g"); 
    var count = (allDataAsString.match(regExp) || []).length; 

    Logger.log('count matches: ' + count); 

    if (count > 1 && arrayOfDups.indexOf(tempStr) === -1) { 
     arrayOfDups.push(tempStr); 
    }; 
    }; 

    Logger.log('arrayOfDups: ' + arrayOfDups); 
    Browser.msgBox('Thest are the duplicate values: ' + arrayOfDups); 

    //To Do - Send Email 
}; 

上面的示例代码有一个硬编码的二维数组用于测试目的。有两个值为'3'的元素出现。