Reputation: 5
I have a google sheet (Sheet 1) which gets auto-populated by a script. Now I want to copy the newly added rows to Sheet 2 only if doesn't exist in Sheet 2. I am trying to match column 1 from both the sheets and if it doesnt exist in sheet 2, the row should be appended in sheet 2. I do not have much experience in google script. Here is my code.
var s1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Original');
var s2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Filtered');
var values1 = s1.getDataRange().getValues();
var values2 = s2.getDataRange().getValues();
var resultArray = [];
for(var n=0; n < values1.length ; n++)
{
var keep = false;
var counter = 0;
for(var p=0; p < values2.length ; p++)
{
if( values1[n][1] == values2[p][1])
{
keep = true;
break ;
}
}
if(keep == false)
{
resultArray.push(values1[n]);
s2.appendRow();
resultArray = [];
keep = false
}
}
}
Upvotes: 0
Views: 1305
Reputation: 1429
You need to pass an argument to appendRow function. You currently append nothing. See here for an example.
var aRow = ["col1", "col2", "col3"];
SpreadsheetApp.getActiveSheet().appendRow( aRow );
On your end, you should have a array which is populated with the values you want to copy for a particular row. If that row does not exist in sheet2, then add it there.
Upvotes: 1