Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script to append range of data in Google Sheets

I have a script that will copy and send a range of data to another sheet. I want to add another part to the script, that will do the same function but append the data to a new row.

  function saveToRecords() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var source = ss.getRange('Report!L1:AC1');
    source.copyTo(ss.getRange('Records!A3:R3'), {contentsOnly: true});
  }

I have this script attached to a button in Google Sheets. The data is sent form Report! to Records!. The Records! sheet will save this data. So each time I click the button I would like the data to be sent to a new row.

I know there is an easy way to do this! I have been stumped. Thanks for your help.

like image 448
Andrew Ramon Medina Avatar asked Oct 16 '25 05:10

Andrew Ramon Medina


1 Answers

yes, there is. You can use getLastRow().

So your code would look like this:

function appendToRecords() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var reportSheet = ss.getSheetByName("Report");
  var recordsSheet = ss.getSheetByName("Records")
  var reportData = reportSheet.getRange("L1:AC1")
                              .getValues();
  var lastRow = recordsSheet.getLastRow();
  //copy data
  recordsSheet.getRange(lastRow + 1, 12, 1, 18)
              .setValues(reportData);

}
like image 182
jvdh Avatar answered Oct 18 '25 04:10

jvdh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!