I have a question with Google Scripting. I just started using Google Script this February so I don't have deep understanding with it yet. Anyway my question is:
Can I delete a row if certain values of it exist in another sheet? I tried several concepts I found in the internet but I was not able to come up with a working function.
Here's the Google Doc for a detailed view.
For example, in Sheet 1 I have two rows:
NAME | PLACE | AGE
Carl | Florida | 45
Mike | Florida | 41
And in Sheet 2:
NAME | PLACE | AGE
Mike | Florida | 41
The script should delete Mike Florida row in Sheet 1 since it has duplicate data in Sheet 2. So basically, if data in Sheet 2 exists in Sheet 1, it should be deleted in Sheet 1. But the tricky part is, I have to compare column 1 and column 2 only. Column 3 is not important. If column 1 and 2 in Sheet 2 have the same exact value in Sheet 1, it should be deleted in Sheet 1 and data in Sheet 2 should remain.
I tried this tutorial in Google site but still I was not able to compare the two sheets and remove the duplicate data.
Thank you so much. Any help/idea/advice will be greatly appreciated. :)
+++++++++
I just came up with a code now and used 2D Arrays Library.
function deleteRowInSheet1() {
var s1 = SpreadsheetApp.openById("COPY SPREADSHEET ID HERE").getSheetByName('Sheet1');
var s2 = SpreadsheetApp.openById("COPY SPREADSHEET ID HERE").getSheetByName('Sheet2');
var values1 = s1.getDataRange().getValues();
var values2 = s2.getDataRange().getValues();
// Check if Mike exists in SS1.
if (ArrayLib.find(values2, 0, 'Mike') != -1) {
for( var row = values1.length -1; row >= 0; --row ) {
if (values1[row][0] == 'Mike')
s1.deleteRow(parseInt(row)+1);
}
}
}
But, this code only gets specific value which is Mike. If row Mike exists in Sheet 2, row Mike in Sheet 1 will be deleted too. I tried modifying the code more to compare the two values from the two different sheets but I can't seem to make it work.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…