Mickus
Mickus

Reputation: 11

Copy a range from last row to new sheet

I want to take the last response from a Google Form and copy it to the same range on another sheet.

The second sheet has some data in columns A & B (a login and password) that I want to assign as somebody completes the registration form.

I want the data from column B to H from the form response sheet to be copied to column C to I on the sheet that contains the login/password columns when a response is received.

I know I am missing the code to take the range from the last row, but I know NO coding at all and my current script is compiled from things I've found around the web and the reference material.

Any advice would be greatly appreciated.

function onEdit(e) {


  var spreadsheet = SpreadsheetApp.openById("sheetid");
  var sheet = spreadsheet.getSheets()[0];


  var lastrow = sheet.getLastRow();
  var range = sheet.getRange(2,2,1,7);
  values = range.getValues();

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var SSsheet = ss.getSheets()[1];


  var ssRange = SSsheet.getRange(2,3,1,7);
  ssRange.setValues(values);

  }

Upvotes: 0

Views: 3040

Answers (2)

Leon K.
Leon K.

Reputation: 115

function onEdit(e) {

var spreadsheet = SpreadsheetApp.openById("ID");
var sheet = spreadsheet.getSheets()[0];
var lastRow = sheet.getLastRow();
var range = sheet.getRange("A"+(lastRow)+":G"+(lastRow));
var values = range.getValues();


var ss = SpreadsheetApp.getActiveSpreadsheet();
var SSsheet = ss.getSheets()[1];
var getRow = SSsheet.getLastRow() + 1;
var ssRange = SSsheet.getRange(getRow, 1, values.length, values[0].length);
ssRange.setValues(values);

Try with this, this code copies the last value you want from a certain range and pastes it in the last available row of the other sheet.
The code it's the same only in the place where you are going to copy it, specify it to be the size of what I want to copy. I hope it helps you :D

Upvotes: 0

ReyAnthonyRenacia
ReyAnthonyRenacia

Reputation: 17613

Try using copyTo(destination). It copies the data from a range of cells to another range of cells. Both the values and formatting are copied.

 // The code below will copy the first 5 columns over to the 6th column.
 var sheet = SpreadsheetApp.getActiveSheet();
 var rangeToCopy = sheet.getRange(1, 1, sheet.getMaxRows(), 5);
 rangeToCopy.copyTo(sheet.getRange(1, 6));
 }

You can also try using copyValuesToRangecopyValuesToRange.

For additional code samples, you may also refer to this forum.

Upvotes: 1

Related Questions