Context
I have been using Google Finance to get my stock market data but sometimes it lags and does not load. So I decided to open up a new tab, and automatically copy and paste the data from the Google Finance tab to the new tab and then use the new tab as my stock data.
I wrote the below code to get the last row of the source sheet of every column to copy and paste it to the last row of the target sheet of every column. Each column has different dimensions (not all stock data starts from the same date so some columns have more data than others).
function myFunction() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sourceSheet = ss.getSheetByName('Live data');
const targetSheet = ss.getSheetByName('Values (Live Data copied)');
// Data from the last row and column of the source sheet
let lastRowS = sourceSheet.getLastRow();
let lastColS = sourceSheet.getLastColumn();
// Data in the last row and column of the target sheet
let lastRowT = targetSheet.getLastRow() + 1;
let lastColT = targetSheet.getLastColumn();
// For every column in the target sheet, we want to copy and paste the values in the last row from every column in the source sheet.
for(var i = 1; i <= lastColT; i++){
// Get data in the last row of the source sheet and paste it into the target sheet.
sourceSheet.getRange(lastRowS, lastColS).setValue(lastRowT, i);
}
}
Problem
How do I copy and paste the data from the last row of every column or where there is data in the source sheet to the last row of every column in the target sheet?
** Update
This is the sheet