检查重复脚本

时间:2015-04-21 22:11:54

标签: javascript google-apps-script google-sheets

我想在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 = "myemail@gmail.com";  // 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 个答案:

答案 0 :(得分: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'。