i Want to compare to values of each cell present in two different google spreadsheet via app script and change the cell's background color to show that cell is updated but i am getting an "Cannot read property "0.0" from undefined. (line 16, file "Code")" i have tried opensheetbyname nd get its ranges still not workring. Googled this error no solution found Here's my code
function myFunction() { var sheet1 = SpreadsheetApp.openById("1-l21- 2B7KZ5sBaRh9kmUFOyeDyP1Pnp1_IqhVlAJJR0"); var lastRow = sheet1.getSheetByName("Sheet1").getLastRow(); var lastCol = sheet1.getSheetByName("Sheet1").getLastColumn(); var data = sheet1.getDataRange().getValues(); var sheet2 = SpreadsheetApp.openById("1YOQjiHZXg86XGmFZ0d5noOoLdowc_JFn3wGjICymi_o"); var lastRow2 = sheet2.getSheetByName("Sheet1").getLastRow(); var lastCol2 = sheet2.getSheetByName("Sheet1").getLastColumn(); var data2 = sheet2.getDataRange().getValues(); for(i = 0; i<=lastRow2; i++){ for(j = 0; j<=lastCol2; j++){ if(data[i][j] != data2[i][j]){ sheet1.getActiveCell().setBackground("#FFFF00"); } } } }
Updated Code
function myFunction() {
var sheet1 = SpreadsheetApp.openById("1Ibyd4QbrAhLMSD_GjIQYEwGCYrbtNTbMKF7vRA-Rc9Q").getSheetByName("Sheet1");
var lastRow = sheet1.getLastRow();
var lastCol = sheet1.getLastColumn();
var data = sheet1.getDataRange().getValues();
var sheet2 =
SpreadsheetApp.openById("1YOQjiHZXg86XGmFZ0d5noOoLdowc_JFn3wGjICymi_o");
var lastRow2 = sheet2.getSheetByName("Sheet1").getLastRow();
var lastCol2 = sheet2.getSheetByName("Sheet1").getLastColumn();
var data2 = sheet2.getDataRange().getValues();
for(i = 3; i<lastRow2; i++){
for(j = 1; j<lastCol2; j++){
if(data[i][j] != data2[i][j]){
sheet1.getRange(i + 1, j + 1).setBackground("#FFFF00");
}
}
}
}
Also i have set trigger on edit for this to work