Copy a range from last row to new sheet - google-apps-script

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);
}

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.

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

Related

apps script - copying manually selected, non-adjacent rows and pasting them below each other

I want to copy manually selected/highlighted non-adjacent rows and paste them directly below each other on another sheet ("fight list raw"). With the script below it works fine, as long as the rows are adjacent. If the rows are non-adjacent, only one row is pasted (the row which was selected lastly / where the cursor is in).
Has someone an idea how to manage it?
function copynonadjacent rows() {
SpreadsheetApp.flush();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.getActiveSpreadsheet();
var source_sheet = ss.getActiveSheet();
var source_range = source_sheet.getActiveRange();
var target_sheet = target.getSheetByName("fight list raw");
var last_row = target_sheet.getLastRow();
target_sheet.insertRowsAfter(last_row,1);
source_range.copyTo(target_sheet.getRange("A"+(last_row+1+":AA"+(last_row+1))),{contentsOnly:true});
}
Thanks a lot!
From your following reply,
I want to copy row number 2 and row number 4. When I select the two rows by clicking on the row numbers and run the script, only row number 4 is copied.
But if I select rows 2 and 3 (so no rows between them), the script copies both rows. In the destination sheet the rows are pasted below each other without a row between them. I want this the same way, when I copy row 2 and 4.
In your script, you use var source_range = source_sheet.getActiveRange();. I thought that this might be the reason for your issue. In this case, how about the following modification?
Modified script:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Source sheet.
var source_sheet = ss.getActiveSheet();
var source_ranges = source_sheet.getActiveRangeList().getRanges();
var values = source_ranges.flatMap(r => r.getValues());
var len = values.length;
// Target sheet.
var target_sheet = ss.getSheetByName("fight list raw");
var last_row = target_sheet.getLastRow();
target_sheet.insertRowsAfter(last_row, len);
target_sheet.getRange(last_row + 1, 1, len, values[0].length).setValues(values);
}
When this script is run, the selected rows are retrieved, and copy the values to the target sheet.
I thought that you use {contentsOnly:true}. So, I thought that getValues and setValues might be able to be used.
Reference:
getActiveRangeList()

Using onEdit trigger, is there a way to make sure the last row in a Sheet is visible to users?

I have a Google Sheet that accumulates up to 100 entries per day. As users add new entries, the script puts new data into the last row (see snippet below). However, there are only 12 rows visible on the screen, so once those are filled, the user can't see new entries.
How can I make the last row visible to the user each time data is entered?
I am new to google-apps-script, so thanks for any ideas. I believe onEdit trigger no longer works with sheet.setActiveRange(), so I am looking for a work-around. If there's no simple solution, I'll take a clunky one. For example, can a script be used to hide rows above the last row?
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SHEET_NAME = 'Sheet1' // Name of current sheet
var sheet = ss.getSheetByName(SHEET_NAME);
var dataCell = sheet.getRange('A1');
var lastRowNumber = sheet.getDataRange().getNumRows();
var newData = dataCell.getValue().split(",");
var lastRow = ss.getLastRow();
sheet.appendRow(newData);
Found a solution: setActiveRange(lastRow+2, lastColumn). Scrolls down far enough, without the need to get the range of the appended row. Added advantage is it scrolls before the row is appended, so user sees data as it goes in.
function onEdit(e) { // Puts csv user data into Sheet1
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SHEET_NAME = 'Sheet1' // Name of current sheet
var sheet = ss.getSheets()[0];
var dataCell = sheet.getRange('A1');
var newData = dataCell.getValue().split(",");
// Gets the address of the very last cell of this sheet
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
// Scrolls to make sure user can see 2 rows below the current last cell
var lastCell = sheet.getRange(lastRow+2, lastColumn);
// Puts user entered data into a new row at the bottom of the spreadsheet
sheet.appendRow(newData);
sheet.setActiveRange(lastCell);
}
You could get the newly added range by adding 1 to the lastrow and activate it:
var lastRow = ss.getLastRow();
sheet.appendRow(newData);
sheet.getRange("A" + (lastRow+1)).activate();//activate the newly appended row's A cell

Cannot find the bug in the CopyRow code from Google Sheets

I'm looking for a code to copy 4 cells for the row that I've selected the cell from one sheet from another sheet of the same spreadsheet, on the last row that is written. The code is this:
function CopiarFila() {
//declaration of sheets
var ss = SpreadsheetApp.openById("ID");
var sheet = ss.getSheetByName("FORD 2019");
var sheetTo = ss.getSheetByName("FORD 2019-F");
//declaration of variables
var row = sheet.getActiveCell().getRow();
var range1 = sheet.getRange("B"+row+":B"+row).getValues();
var range2 = sheet.getRange("C"+row+":C"+row).getValues();
var range3 = sheet.getRange("F"+row+":F"+row).getValues();
var range4 = sheet.getRange("I"+row+":I"+row).getValues();
//declaration of the last row from the new sheet
var ultimaFila = sheetTo.getLastRow();
//adding a row after the last one
sheetTo.insertRowsAfter(ultimaFila, 1);
//copying values to the last row
sheetTo.getRange("B"+ultimaFila+":B"+ultimaFila).setValues(range1);
sheetTo.getRange("D"+ultimaFila+":D"+ultimaFila).setValues(range2);
sheetTo.getRange("C"+ultimaFila+":C"+ultimaFila).setValues(range3);
sheetTo.getRange("H"+ultimaFila+":H"+ultimaFila).setValues(range4);`
}
As I see when running the code, the row is created so the problem isn't from the commands: Sheet, SheetTo and ultimaFila. Am I using properly the command '.getValues('?
I'm stuck and I cannot find a way to fix it. Thank you for your help!
IF you don't mind the blank columns, there is an easier way to do this,
var ss1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("FORD 2019");
var ss2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("FORD 2019-F");
ss1.getRange("B:B").copyTo(ss2.getRange("B:B"));
ss1.getRange("C:C").copyTo(ss2.getRange("D:D"));
ss1.getRange("F:F").copyTo(ss2.getRange("C:C"));
ss1.getRange("I:I").copyTo(ss2.getRange("H:H"));

Copy Row and Delete

I have 2 sheets - 'Form Responses 1' and 'Students'.
When I run the function below, it copies all data from 'Form Responses 1' to 'Students' where I'll need to massage the data further in the second sheet. After I copied, I want that particular row to be deleted from 'Form Responses 1'. The next round when I run the function again (when there is new data in 'Form Responses 1'), it will append to 'Students' accordingly.
Would very much appreciate if you could advise how can I amend my codes below, thanks!
function Copy()
{
var sss = SpreadsheetApp.openById('sheet ID'); //replace with source ID
var ss = sss.getSheetByName('Form Responses 1'); //replace with source Sheet tab name
var range = ss.getRange('A:V'); //assign the range you want to copy
var data = range.getValues();
var numRows = range.getNumRows();
var getRow = range.getRow();
var tss = SpreadsheetApp.openById('sheet ID'); //replace with destination ID
var ts = tss.getSheetByName('Students');
ts.getRange(1, 1, data.length, data[0].length).setValues(data);
/*delete function is missing*/
}
The following bit of code should achieve the desire result, otherwise it is a good place to start. The code is pretty simple, so I put all the explanations in the comments of the code.
To use this code you can create a Stand alone script. (Does not need to be bound to a spreadsheet.) Copy Primary and Secondary code into two seperate tabs. (See the attached picture.) Once you have changed the IDs in the two scripts, run the primary script and you should see the desired result. (Be aware the all data in the form responses will be cleared.)
Hope this helps, and feel free to ask if you are not sure. Just note at this point I will be directing you to the documentation instead of trying to explain how certain methods/calls work:
Primary Code
function copy_Delete(){
var ss, sheet;
var lastRow, lastColumn;
var responseData;
//Get Spreadsheet and Sheet name for Form Responses Sheet
ss = SpreadsheetApp.openById("YOUR_ID_HERE");
sheet = ss.getSheetByName("Form Responses 1");
//Gets a numerical value representing the last row and last column where data is entered.
lastRow = sheet.getLastRow();
lastColumn = sheet.getLastColumn();
//Get all the data in the From Responses Sheet and put it all in an array.
responseData = sheet.getRange(2, 1, lastRow -1, lastColumn).getValues();
//Call the destination function and pass the array to it. Then clear form responses of all data.
destination(responseData);
sheet.clear();
}
Secondary Code
function destination(responseData){
var ss, sheet, form;
var numRows, numColumns, lastRow;
//Get Spreadsheet and Sheet name for your destination sheet
ss = SpreadsheetApp.openById("YOUR ID HERE");
sheet = ss.getSheetByName("Destination Sheet");
//Get the dimensions of the array and the last row of the spreadsheet.
numRows = responseData.length;
numColumns = responseData[0].length;
lastRow = sheet.getLastRow();
//Using the dimensions above,we push our array from the previous function into our new sheet.
sheet.getRange(lastRow +1, 1, numRows, numColumns).setValues(responseData);
//Get form and delete all responses.
form = FormApp.openById('YOUR_FORM_ID_HERE');
form.deleteAllResponses();
}
Attached example

Copy Columns from a sheet to add at lastcolumn in "archive" sheet with GAS

Sorry for my english. ^^
Erm, i´ve found a script to copy from a sheet to an other Sheet.
My problem is: its copy with rows.
I want to copy columns and add 2 columns to lastcolumn on script running.
It becames a problem, because columns has no a1notification. A=1, B=2, etc.
copyTo wants A1notification.
Heres my script:
function Archiv() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.openById("sheetid");
var source_sheet = ss.getSheetByName("sheetname");
var target_sheet = target.getSheetByName("sheetnametarget");
var source_range = source_sheet.getRange("D4:E89");
var last_col = target_sheet.getLastColumn();
target_sheet.insertColumnAfter(last_col);
var target_range = target_sheet.getRange(">>>>(lastcol+1)<<<<4:>>>>>(lastcol+2)<<<<89");
source_range.copyTo(target_range, {contentsOnly:true});
}
The script have to run every Monday to archive the content of 2 Columns.
I hope you can understand my wish.
I have tested many things, but nothing helps. :(
Many thanks in advance.
Sebastian
You can use another range selection mode that will makes things very simple, see this doc, using integers for row and column numbers.
Your code could become like this :
function Archiv() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.openById("sheetid");
var source_sheet = ss.getSheetByName("sheetname");
var target_sheet = target.getSheetByName("sheetnametarget");
var source_range = source_sheet.getRange("D4:E89");
var last_col = target_sheet.getLastColumn();
while(target_sheet.getMaxColumns()<=last_col+2){// add columns only if necessary
target_sheet.insertColumnAfter(last_col)}
var source_values = source_range.getValues();
var target_range = target_sheet.getRange(4,last_col+1,source_values.length,2);// row Nr4, starting on last column+1, 2 columns width and 89-4 rows heigth
target_range.setValues(source_values);
}
I used getValues() and setValues() instead of copyTo() that returned an error : target and source must be in the same spreadsheet