【发布时间】:2020-01-03 05:02:36
【问题描述】:
我正在尝试做一些看似简单的事情。我有一张巨大的 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.");
}
};
谢谢!
【问题讨论】:
-
您的代码有什么问题 - 它没有按预期工作,您是否遇到任何错误?
-
感谢您的回复!我对其进行了编辑以显示问题代码是什么,以及发生了什么。
标签: javascript google-apps-script google-sheets