Google-sheets – How to copy range from one sheet to another column with offset

google sheets

I have a range of data on sheet5 which needs to be copied and inserted 27 columns in from the end of sheet1. I can get it to insert the column in the right place but it doesn't copy the data. I am new to scripting so any advice would be of great help.

Here is code I am using:

function store() {
  var ss = SpreadsheetApp.getActiveSpreadsheet ();

  var source = ss.getRange ("Sheet5!D4:D80");

  var destSheet = ss.getSheetByName("Sheet1");

  var lastCol = destSheet.getLastColumn() -27;

  destSheet.insertColumnAfter(lastCol);

  source.copyTo(destSheet.getRange(lastCol + 1,1), {contentsOnly: true});

  SpreadsheetApp.getUi().alert('Played today Recorded');
}

Best Answer

Isn't it always the case. You spend hours trying to figure something out, then as soon as you ask someone the answer arrives. For anyone wondering, here is the correct code.

function store() {
  var ss = SpreadsheetApp.getActiveSpreadsheet ();

  var source = ss.getRange ("Sheet5!D4:D80");

  var destSheet = ss.getSheetByName("Sheet1");

  var lastCol = destSheet.getLastColumn() -27;

  destSheet.insertColumnAfter(lastCol);

  source.copyTo(destSheet.getRange(4,lastCol +1,80), {contentsOnly: true});

   SpreadsheetApp.getUi().alert('Played today Recorded');
}