I have the working code below that compares two sets of data from two different sheets. One is a list of 395 phone numbers, the second is a list of around 135,000 rows of data. The following code compares the two for similarities and copies the similar row to a third sheet.
While the code does work for small scale tests, (1000 rows and 395 phone numbers) in around 10 minutes. However doing 135,000 rows makes the script time out. Here is the script below.
`function copyRowtoSheet3() {
var s1 = SpreadsheetApp.openById("1Aw11LiKzyezfrTQIuTsJPhUFtz8RPqLCc8FlIiy0ZlE").getSheetByName('Sheet1');
var s2 = SpreadsheetApp.openById("1Aw11LiKzyezfrTQIuTsJPhUFtz8RPqLCc8FlIiy0ZlE").getSheetByName('Sheet2');
var s3 = SpreadsheetApp.openById("1Aw11LiKzyezfrTQIuTsJPhUFtz8RPqLCc8FlIiy0ZlE").getSheetByName('Sheet3');
var values1 = s1.getDataRange().getValues();
var values2 = s2.getDataRange().getValues();
var resultArray = [];
for(var n=0; n < values1.length ; n++){
var keep = false;
for(var p=0; p < values2.length ; p++){
Logger.log(values1[n][0]+' =? '+values2[p][0]);
if( values1[n][1] == values2[p][0] ){
resultArray.push(values1[n]);
Logger.log('true');
//break ;
}
}
}
s3.getRange(+1,1,resultArray.length,resultArray[0].length).setValues(resultArray);
}`
I'm not the best at optimizing code, and would love it if there were any suggestions that could be inputted here. I took a look at best practices and applied them already to the best of my abilities. Any suggestions to the code here?