简体   繁体   English

如何使用谷歌脚本检查两个谷歌工作表,并将重复项移动到新工作表?

[英]How to use google scripts to check two google sheets, and move the duplicates to a new sheet?

I'm trying to do something which seems simple.我正在尝试做一些看起来很简单的事情。 I have a gigantic sheet of 20k contacts, and there are contacts with bad emails.我有一张巨大的 20k 联系人表,并且有一些联系人的电子邮件很差。 I've compiled a list of the bad emails I want to pull, and want to write a script which finds the emails from the "bad" list in the "20k" list, copies the whole row of each bad email to a "new" (generated) sheet, and then delete the row from the list of 20k.我已经编制了一个我想提取的坏邮件列表,并想编写一个脚本,从“20k”列表中的“坏”列表中查找电子邮件,将每封坏邮件的整行复制到“新” "(生成)表,然后从 20k 列表中删除该行。

Everything works until it needs to check for duplicates, copy them and delete the old ones (a nested for loop).一切正常,直到它需要检查重复项,复制它们并删除旧的(嵌套的 for 循环)。 Right now it copies everything (duplicate or not)multiple times, and then deletes the entire sheet.现在它会多次复制所有内容(重复与否),然后删除整个工作表。 Here is the problem code:这是问题代码:

 // Find duplicates from the two sheets and move them to the "FindDupes" sheet
  var dupes = false;
  var dataMDS = sourceSheetMDS.getDataRange().getValues();
  var dataETR = sourceSheetETR.getDataRange().getValues();
  for (i = numETRRows; i >= 0; i--) {
    for (j = numMDSRows; j >= 0; j--) {
      if  (sourceSheetETR[i,1] == sourceSheetMDS[j,1]) {
        dupes = true;

        // Copy the desired rows to the FindDupes sheet
        for (var k = 1; k <= numMDSCols; k++) {
          var sourceRange = sourceSheetMDS.getRange(1,k,j);
          var nextCol = newSheet.getLastColumn() + 1;
          sourceRange.copyTo(newSheet.getRange(1,nextCol,j));
        }
        sourceSheetMDS.deleteRow(j);
      }
    }
  }

Here is the whole project:这是整个项目:

function findDuplicates() {

  // List the columns you want to check by number (A = 1)
  var CHECK_COLUMNS = [1];

  //Declare the Spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();  

  // Get the active sheet and info about it
  // Main Database Sheet
  var sourceSheetMDS = ss.getSheetByName("test");
  var numMDSRows = sourceSheetMDS.getLastRow();
  var numMDSCols = sourceSheetMDS.getLastColumn();

  // Get the active sheet and info about it
  // Emails To Rremove Sheet
  var sourceSheetETR = ss.getSheetByName("Emails to Remove");
  var numETRRows = sourceSheetETR.getLastRow();
  var numETRCols = sourceSheetETR.getLastColumn();

  // Create the sheet of duplicates
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var newSheet = ss.insertSheet("FindDupes");

  // Find duplicates from the two sheets and move them to the "FindDupes" sheet
  var dupes = false;
  var dataMDS = sourceSheetMDS.getDataRange().getValues();
  var dataETR = sourceSheetETR.getDataRange().getValues();
  for (i = numETRRows; i >= 0; i--) {
    for (j = numMDSRows; j >= 0; j--) {
      if  (sourceSheetETR[i,1] == sourceSheetMDS[j,1]) {
        dupes = true;

        // Copy the desired rows to the FindDupes sheet
        for (var k = 1; k <= numMDSCols; k++) {
          var sourceRange = sourceSheetMDS.getRange(1,k,j);
          var nextCol = newSheet.getLastColumn() + 1;
          sourceRange.copyTo(newSheet.getRange(1,nextCol,j));
        }
        sourceSheetMDS.deleteRow(j);
      }
    }
  }


  // Alert the user with the results
  if (dupes) {
    Browser.msgBox("Possible duplicate(s) found, moved, and deleted.");
  } else {
    Browser.msgBox("No duplicates found.");
  }
};

Thank you!谢谢!

Change改变

if  (sourceSheetETR[i,1] == sourceSheetMDS[j,1]) {
        dupes = true;
...

to

if  (dataETR[i,1] == dataMDS[j,1]) {
        dupes = true;
...

Explanation:解释:

You have to compare values and not (not existant) sheet array entries.您必须比较值而不是(不存在的)工作表数组条目。

I had to change a few things around because I couldn't get it to run properly.我不得不改变一些东西,因为我无法让它正常运行。

 function findDuplicates() { // List the columns you want to check by number (A = 1) var CHECK_COLUMNS = [1]; //Declare the Spreadsheet var ss = SpreadsheetApp.getActiveSpreadsheet(); // Get the active sheet and info about it // Main Database Sheet var sourceSheetMDS = ss.getSheetByName("test"); var numMDSRows = sourceSheetMDS.getLastRow(); var numMDSCols = sourceSheetMDS.getLastColumn(); // Get the active sheet and info about it // Emails To Rremove Sheet var sourceSheetETR = ss.getSheetByName("Emails to Remove"); var numETRRows = sourceSheetETR.getLastRow(); var numETRCols = sourceSheetETR.getLastColumn(); // Create the sheet of duplicates var newSheet = ss.insertSheet("FindDupes"); // Find duplicates from the two sheets and move them to the "FindDupes" sheet var dupes = false; var dataMDS = sourceSheetMDS.getDataRange().getValues().reverse(); var dataETR = sourceSheetETR.getDataRange().getValues(); var rowsToDelete = [] dataETR.forEach(function (emailToRemoveRow) { var emailToRemove = emailToRemoveRow[0] dataMDS.forEach(function (dataRow, j) { var emailOfData = dataRow[0] if (emailToRemove == emailOfData) { dupes = true; // Copy the desired rows to the FindDupes sheet newSheet.appendRow(dataRow) rowsToDelete.push(j) } }) }) rowsToDelete.sort(function (a, b) { return b - a; }).forEach(function (rowIndex) { sourceSheetMDS.deleteRow(rowIndex); }) // Alert the user with the results if (dupes) { Browser.msgBox("Possible duplicate(s) found, moved, and deleted."); } else { Browser.msgBox("No duplicates found."); } };

and I am still a little confused because check_columns is not getting used.我仍然有点困惑,因为 check_columns 没有得到使用。 So where exactly is the email column?那么电子邮件列究竟在哪里? And what does the emails to remove sheet looks like?删除工作表的电子邮件是什么样的? This code above is assuming that the email to remove sheet only has a single column which has the emails we want to remove, and the test sheet has the emails in its first column.上面的代码假设要删除的电子邮件只有一列包含我们要删除的电子邮件,而测试表的第一列中有电子邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM