Automate Google Sheets - Javascript Help

Joined
Apr 7, 2016
Messages
342
Likes
233
Degree
1
I have a Google sheet that pulls data in one tab "GA Data" and I have it copy (currently after pressing the Copy Historic Data button in the header) to another tab "Historic Data" but the problem is when I copy it over it overwrites the whole range. I'm trying to figure out how to get it to copy into the last row of "Historic Data". Can anybody point me in the right direction?

Code:
function onOpen() {
   var ss = SpreadsheetApp.getActiveSpreadsheet();
  var items = [
    {name: 'Copy to Historic Data', functionName: 'CopyDataToNewTab'},
 
  ];
  ss.addMenu('Copy Data', items);
}

function CopyDataToNewTab() {
  var sss = SpreadsheetApp.openById('1fDiSLAHT1yU855xNpX_k2b_0lUsTVPhYpaATDAJ5BQs'); // sss = source spreadsheet
  var ss = sss.getSheetByName('GA Data'); // ss = source sheet
  //Get full range of data
  var SRange = ss.getDataRange();
  //get A1 notation identifying the range
  var A1Range = SRange.getA1Notation();
  //get the data values in range
  var SData = SRange.getValues();

  var tss = SpreadsheetApp.openById('1fDiSLAHT1yU855xNpX_k2b_0lUsTVPhYpaATDAJ5BQs'); // tss = target spreadsheet
  var ts = tss.getSheetByName('Historic Data'); // ts = target sheet
  //set the target range to the values of the source data
  //ts.insertRowAfter(3);
 
  var last_row = ts.getLastRow();
  ts.insertRowAfter(last_row);
 
  ts.getRange(A1Range).setValues(SData);

}
 
This is where I'm trying to get it to show on the last row:
Code:
var last_row = ts.getLastRow();
  ts.insertRowAfter(last_row);
 
Back