Maybe this is what you are looking for. Keep in mind that it's not possible to check for the Master data
in the second sheet
without using the name in Z1
, unless you manually input the name of the second Sheet in the code first.
function main(){
//Master Sheet
var sprsheet = SpreadsheetApp.getActiveSpreadsheet();
var master_sheet = sprsheet.getSheetByName("ENTRATE MAIN");
var master_range = master_sheet.getRange("AJ2:J50");
//Second Sheet
var sheetName = master_sheet.getRange("Z1").getValue();
var second_sheet = sprsheet.getSheetByName(sheetName);
var lastrow = second_sheet.getLastRow();
var master_data = master_range.getValues();
if (lastrow > 50){ //This is in case your second sheet has less than 50 rows before copying the data
//We have to check the previous 49 rows in case the data is already there
var second_range = second_sheet.getRange("AJ"+(lastrow-49)+":J"+(lastrow));
var second_data = second_range.getValues();
if (!isCopied(master_data, second_data)){
//Data is not there so we copy it in the next 49 rows
//If you want to overwrite the last row, just remove the +1
second_range = second_sheet.getRange("AJ"+(lastrow+1)+":J"+(lastrow+49));
second_range.setValues(master_data);
}
} else {
//The second sheet has less than 50 rows so the data is obviously not there
var second_range = second_sheet.getRange("AJ"+(lastrow+1)+":J"+(lastrow+49));
second_range.setValues(master_data);
}
}
function isCopied(master_data, second_data){
for (i in master_data){
for (j in master_data[i]){
if (master_data[i][j] != second_data[i][j]){
return false;
}
}
}
return true;
}