0
votes

I selected manually a range of data and want to press a button to copy those data to after the last row of data in the same sheet using Google Apps Script but not working at all. How to fix it? The codes are:

var listToCopy = SpreadsheetApp.getActiveSheet().getActiveRange();
  var height = listToCopy.getHeight();
  var destRow = listToCopy.getLastRow()+1;
  listToCopy.copyTo(spreadsheet.getRange(destRow, 0, height));
1

1 Answers

1
votes

I believe your goal as follows.

  • You want to copy the selected range to the last row of the same sheet using Google Apps Script.

For this, how about this answer?

Modification points:

  • In the case of var destRow = listToCopy.getLastRow()+1;, the last row of the range of listToCopy is retrieved.
    • In your case, the last row of the sheet is required to be retrieved.
  • The source range can be copied to the destination range as the start range like getRange(destRow, 1).
    • The 1st number of range is 1.
  • spreadsheet is not declared.

When above points are reflected to your script, it becomes as follows.

Modified script:

In order to use this script, please select the range and run the function of myFunction. By this, the selected range is copied to the last row of the same sheet.

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var listToCopy = sheet.getActiveRange();
  var destRow = sheet.getLastRow() + 1;
  listToCopy.copyTo(sheet.getRange(destRow, 1));
}

References: