簡體   English   中英

如何使用谷歌腳本檢查兩個谷歌工作表,並將重復項移動到新工作表?

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

我正在嘗試做一些看起來很簡單的事情。 我有一張巨大的 20k 聯系人表,並且有一些聯系人的電子郵件很差。 我已經編制了一個我想提取的壞郵件列表,並想編寫一個腳本,從“20k”列表中的“壞”列表中查找電子郵件,將每封壞郵件的整行復制到“新” "(生成)表,然后從 20k 列表中刪除該行。

一切正常,直到它需要檢查重復項,復制它們並刪除舊的(嵌套的 for 循環)。 現在它會多次復制所有內容(重復與否),然后刪除整個工作表。 這是問題代碼:

 // 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);
      }
    }
  }

這是整個項目:

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.");
  }
};

謝謝!

改變

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

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

解釋:

您必須比較值而不是(不存在的)工作表數組條目。

我不得不改變一些東西,因為我無法讓它正常運行。

 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."); } };

我仍然有點困惑,因為 check_columns 沒有得到使用。 那么電子郵件列究竟在哪里? 刪除工作表的電子郵件是什么樣的? 上面的代碼假設要刪除的電子郵件只有一列包含我們要刪除的電子郵件,而測試表的第一列中有電子郵件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM