I have two spreadsheets. I want to match column C of spreadsheet1
with column A of spreadsheet2
. Both of these spreadsheet has records more than 8000. Due to huge number of records my script constantly gives exceeded maximum execution time
error. Here is the script
function compare() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheetList1=ss.getSheetByName("spreadsheet1");
var sheetList2=ss.getSheetByName("spreadsheet2");
var sheet1Data=sheetList1.getRange(2,3,sheetList1.getLastRow(),1).getValues();
var sheet2Data=sheetList2.getRange(1,1,sheetList2.getLastRow(),1).getValues();
for (i in sheet2Data){
var row = 2;
for (j in sheet1Data){
if (sheet1Data[j][0]==sheet2Data[i][0]){
sheetList1.getRange("A"+row).setValue('Inactive');
}
row++;
}
}
}
any suggestions for optimizing this script. Or how to handle this error ? Thanks in advance :)
EDIT
Thanks for the wonderful reply. There is one issue. If I push data in newSheet1Data
array before the if
statement then it write Inactive
twice. i.e if there are two rows it writes inactive
to four rows.Like
newSheet1Data.push(sheet1Data[j]);
if (sheet1Data[j][2]==sheet2Data[i][0]){
newSheet1Data[j][0]='Inactive';
}
If I push data inside if
statement and no match occur then it does not find row and give this error TypeError: Cannot set property "0.0" of undefined to "Inactive"
. Like
if (sheet1Data[j][0]==sheet2Data[i][0]){
newSheet1Data.push(sheet1Data[j]);
newSheet1Data[j][0]='Inactive';
}