通过仅比较某些列上的某些值来删除 Google sheet 上的重复行
Delete Duplicate rows on Google sheet by comparing only certain values on certain columns
Sheet 1
Timestamp |Name | Shift.....| Date....| Letter..|
19/05/2016 15:44:46|Mark | Swap | 02/AUG | S |
Timestamp2 |Ivon | Give away | 25/ AUG | G |
Timestamp3 |Laura| Take | 01/AUG | A |
Timestamp4 |Ann | Take | 02/AUG | A |
Timestamp5 |Ann | Give away | 03/AUG | G |
Timestamp6 |Laura| Delete | 01/AUG | D |
因为这是 google sheet,我正在尝试 运行 下面的脚本来删除匹配名称和日期列的行。例如。 Laura 在 01AUG 的同一天有 2 个条目。因此我想删除这两行。我正在尝试以下脚本,但它不起作用。
function removeDuplicates() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Form responses 2");
var data = sheet.getDataRange().getValues();
var newData = new Array();
for(i in data){
var row = data[i];
var duplicate = false;
for(j in newData){
if( row[0] == newData[j][1] && row[1] == newData[j][3]){
duplicate = true;
}
}
if(!duplicate){
newData.push(row);
}
}
sheet.clearContents();
sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData);
}
v4。决赛(我认为)。
如果Cx是"delete"或"Delete"删除行x和B和D与行x相同的行。
function objTst(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Form responses 2");
var megarng = sheet.getRange(2, 2, sheet.getLastRow(),sheet.getLastColumn()).getValues(); //[][]
var rodel = [0]; //rows to delete
for (i = 0; i<megarng.length; i++){
if (String(megarng[i][1])==("Delete"||"delete")){ //megarng[][x] 0=b, 1=c, 2=d
rodel.push(i+2)
for (g=0; g<megarng.length ; g++){
if(String(megarng[g][0])===String(megarng[i][0]) && String(megarng[g][2])===String(megarng[i][2])){
rodel.push(g+2);
Logger.log(megarng[g][0]);
Logger.log(megarng[i][0]);
Logger.log(megarng[g][2]);
Logger.log(megarng[i][2]);
Logger.log(i+"i");
Logger.log(g);
}
}
}
rodel.sort(function comp(a,b){return a-b});
Logger.log(rodel);
}
for (i=rodel.length-1 ; 0<i; i--){if (rodel[i-1]!==rodel[i]){sheet.deleteRow(rodel[i]);}}
}
Sheet 1
Timestamp |Name | Shift.....| Date....| Letter..|
19/05/2016 15:44:46|Mark | Swap | 02/AUG | S |
Timestamp2 |Ivon | Give away | 25/ AUG | G |
Timestamp3 |Laura| Take | 01/AUG | A |
Timestamp4 |Ann | Take | 02/AUG | A |
Timestamp5 |Ann | Give away | 03/AUG | G |
Timestamp6 |Laura| Delete | 01/AUG | D |
因为这是 google sheet,我正在尝试 运行 下面的脚本来删除匹配名称和日期列的行。例如。 Laura 在 01AUG 的同一天有 2 个条目。因此我想删除这两行。我正在尝试以下脚本,但它不起作用。
function removeDuplicates() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Form responses 2");
var data = sheet.getDataRange().getValues();
var newData = new Array();
for(i in data){
var row = data[i];
var duplicate = false;
for(j in newData){
if( row[0] == newData[j][1] && row[1] == newData[j][3]){
duplicate = true;
}
}
if(!duplicate){
newData.push(row);
}
}
sheet.clearContents();
sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData);
}
v4。决赛(我认为)。
如果Cx是"delete"或"Delete"删除行x和B和D与行x相同的行。
function objTst(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Form responses 2");
var megarng = sheet.getRange(2, 2, sheet.getLastRow(),sheet.getLastColumn()).getValues(); //[][]
var rodel = [0]; //rows to delete
for (i = 0; i<megarng.length; i++){
if (String(megarng[i][1])==("Delete"||"delete")){ //megarng[][x] 0=b, 1=c, 2=d
rodel.push(i+2)
for (g=0; g<megarng.length ; g++){
if(String(megarng[g][0])===String(megarng[i][0]) && String(megarng[g][2])===String(megarng[i][2])){
rodel.push(g+2);
Logger.log(megarng[g][0]);
Logger.log(megarng[i][0]);
Logger.log(megarng[g][2]);
Logger.log(megarng[i][2]);
Logger.log(i+"i");
Logger.log(g);
}
}
}
rodel.sort(function comp(a,b){return a-b});
Logger.log(rodel);
}
for (i=rodel.length-1 ; 0<i; i--){if (rodel[i-1]!==rodel[i]){sheet.deleteRow(rodel[i]);}}
}