1
votes

I use the following cope in google sheets to copy a range of values from a row on a source sheet. It appends it to the destination sheet as values starting in the 2nd column. This works fine, but i now want to add on column 1 the date/time "localDateTime" that the script runs, but i just don't know how to get this done.

Can somebody help please

function copyInfo() {
  // get date & time
  var localDateTime = new Date()
  // other variables
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var copySheet = ss.getSheetByName("Performance");
  var pasteSheet = ss.getSheetByName("PortfolioTrend");

  // get source range
  // getRange(row, column, numRows, numColumns)
  var source = copySheet.getRange(103,5,1,7);

  // get destination range
  var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,+2,1,7);


  // copy values to destination range
  source.copyTo(destination, {contentsOnly:true});

}
1

1 Answers

2
votes

I believe your goal as follows.

  • You want to put the value of localDateTime to a left side of the 1st column of destination.

For this, how about this modification?

From:

source.copyTo(destination, {contentsOnly:true});

To:

source.copyTo(destination, {contentsOnly:true});
destination.offset(0, -1, 1, 1).setValue(localDateTime);  // Added

Reference: