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));
I believe your goal as follows.
For this, how about this answer?
var destRow = listToCopy.getLastRow()+1;
, the last row of the range of listToCopy
is retrieved.
getRange(destRow, 1)
.
1
.spreadsheet
is not declared.When above points are reflected to your script, it becomes as follows.
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));
}