Copy row to new sheet using Google Apps Script - google-apps-script

I know it has been asked and answered here many times, but I can not make it work. I am trying to copy a row values (no formulas) from one sheet to another sheet on googlesheets using Google Apps Script. Very basic, but somehow it is giving me hard time.
I have:
SourceSheet
SourceWorksheet
TargetSheet
TargetWorksheet
I am trying to Copy row values (no formulas) from SourceWorksheet(A2:K2), to a new row (after last used row) on TargetWorksheet. It will be again from (A:K) but row number will be different, each time it will create a new row after last row.
Any help would be really appreciated!
EDIT
I found this code below, but not sure how to modify it to read the range i mentioned above instead of all the rows from source worksheet, and I need to modify it so it finds last row on target worksheet then creates new row each time to write the values ( so it doesn't overwrite any data on target)
function myFunction() {
var source = SpreadsheetApp.openById('xxxxxx');
var sourcesheet = source.getSheetByName('sheet1');
var target = SpreadsheetApp.openById('xxxxx')
var targetsheet = target.getSheetByName('sheet1');
var targetrange = targetsheet.getRange(2, 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn());
var rangeValues = sourcesheet.getRange(2, 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn()).getValues();
targetrange.setValues(rangeValues);
}

To add a line to the last row of a form, use the sheet.appendRow() method.
In your example, var targetRange would not be needed. You could simply use targetSheet.appendRow(rangeValues) where the rangeValues = the range you're copying (as an array)
function myFunction() {
var source = SpreadsheetApp.openById('xxxxxx');
var sourcesheet = source.getSheetByName('sheet1');
var target = SpreadsheetApp.openById('xxxxx')
var targetsheet = target.getSheetByName('sheet1');
var rangeValues = sourcesheet.getRange(2, 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn()).getValues();
targetSheet.appendRow(rangeValues);
}

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()

The Google Sheets script I'm using is copying a named Range, but to the wrong column

I'm using a modified Google Sheets script I found on here to keep a named range synced across two tabs of a document. However, when an edit happens, it just copies the named range ("Attendance") from Sheet2 to the first column of Sheet1, as opposed to the same position in Sheet1. (Basically, the range it's copying from in Sheet2 is Column I, but it copies into Column A in Sheet1, overwriting the data there.)
What can I add so it goes to the right place?
var sourceSpreadsheetID = "1SrT3N_DI1vLeh-ACisZgREA5gMPjAWQu8edQtWXC3lM";
var sourceWorksheetName = "Sheet2";
var targetSpreadsheetID = "1SrT3N_DI1vLeh-ACisZgREA5gMPjAWQu8edQtWXC3lM";
var targetWorksheetName = "Sheet1";
function onEdit(e) {
var thisSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetID);
var thisWorksheet = thisSpreadsheet.getSheetByName(sourceWorksheetName);
var thisData = thisSpreadsheet.getRangeByName("Attendance");
var toSpreadsheet = SpreadsheetApp.openById(targetSpreadsheetID);
var toWorksheet = toSpreadsheet.getSheetByName(targetWorksheetName);
var toRange = toWorksheet.getRange(1, 1, thisData.getNumRows(), thisData.getNumColumns())
toRange.setValues(thisData.getValues());
}
Use getA1Notation() to retrieve the A1 notation of the named range, and use it for your target range.
So, replace this:
var toRange = toWorksheet.getRange(1, 1, thisData.getNumRows(), thisData.getNumColumns());
With this:
var toRange = toWorksheet.getRange(thisData.getA1Notation());
Notes:
If you want to copy the range (formatting included) and not just the values, and if both ranges are on the same spreadsheet, use copyTo(destination) instead of setValues.
If the spreadsheet/s you are working is not the container of your script, this script might need authorization, in which case I'd suggest you to install the onEdit trigger.
const vA=thisData.getValues();
var toRange = toWorksheet.getRange(1, 2, vA.length, vA[0].length);
This is because your getRange is set to Row 1, Column 1 hence when you change it to desired row and column you will get the data placed in specified row and column
var toRange = toWorksheet.getRange(1, 9, thisData.getNumRows(), thisData.getNumColumns())
Hope this solves your issue.

Why is this copying to the new sheet at varying intervals?

I'm copying data from one sheet to another 3x per week. The triggers are working fine but the data is supposed to copy to a new row sequentially. However, right now it's copying with varying numbers of rows in between them and I cant figure out why.
I have tried deleting all of the data in the sheet, deleting the sheet and recreating it, and double checked how I've input what data needs to be copied.
var logSheet = ss.getSheetByName('Sheet1');
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName('Data Sheet');
var RDSheet = ss.getSheetByName('RawDataSheet');
var lastRow = logSheet.getLastRow();
var RawData = sourceSheet.getRange(45,1,1,34);
RawData.copyTo(RDSheet.getRange(lastRow + 1, 1), {contentOnly: false});
I expect the range (45,1,1,34), one long row, to copy itself to the RawDataSheet and when it triggers it places the data 1 row below the previously copied data.
Try this:
function myfunction() {
var ss=SpreadsheetApp.getActive();
var logSheet=ss.getSheetByName('Sheet1');
var sourceSheet=ss.getSheetByName('Data Sheet');
var RDSheet=ss.getSheetByName('RawDataSheet');
var lastRow=logSheet.getLastRow();
var RawData=sourceSheet.getRange(45,1,1,34);
RawData.copyTo(logSheet.getRange(lastRow + 1, 1), {contentOnly: false});
}

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

Copy/paste incremental data from one tab to another tab

I have little coding experience but can usually cobble together a solution by looking at examples. This time have not been able to get it working or found examples within my skill range to parse.
Essentially I'd like to copy data from tab 1 and place it underneath the running data on tab 2, but only when there is data on tab 1 to copy.
I started with the following which does work, but doesn't check for available data, or vary the size the of the range of data captured so it runs and pastes a lot of blank space.
function CopyRows() {
var source = SpreadsheetApp.openById('key');
var sourcesheet = source.getSheetByName('ImportPage');
var target = SpreadsheetApp.openById('key')
var targetsheet = target.getSheetByName('TargetPage');
var targetrange = targetsheet.getRange(targetsheet.getLastRow() + 1 , 1);
sourcesheet.getRange(2, 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn()).copyTo(targetrange);}
I think that what I need to add to this is a way to vary the range of data captured, and to halt the function if no data is present.
I have the data coming in from another sheet though the importrange function. it might be better to do the whole thing via script but that part is working well enough for now.
Any advice would be appreciated.
This might work:
function CopyRows() {
var source = SpreadsheetApp.openById('key');
var sourcesheet = source.getSheetByName('ImportPage');
var target = SpreadsheetApp.openById('key')
var targetsheet = target.getSheetByName('TargetPage');
var lastRowOfTrgtSheet = targetsheet.getLastRow();
var srcData = sourcesheet
.getRange(2, 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn())
.getValues();
targetsheet.getRange(lastRowOfTrgtSheet+1, 1, srcData.length, srcData[0].length)
.setValues(srcData);
};
It uses the setValues() method instead of copyTo().
If you want to use copyTo(), you'll need to use the version of getRange() that takes 4 parameters, and subtract 1 from the length of the rows in the source sheet:
function CopyRows() {
var source = SpreadsheetApp.openById('key');
var sourcesheet = source.getSheetByName('ImportPage');
var target = SpreadsheetApp.openById('key')
var targetsheet = target.getSheetByName('TargetPage');
var lastRowOfTrgtSheet = targetsheet.getLastRow();
var srcLastRw = sourcesheet.getLastRow();
var targetrange = targetsheet
.getRange(lastRowOfTrgtSheet + 1, 1, srcLastRw-1, sourcesheet.getLastColumn());
sourcesheet.getRange(2, 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn())
.copyTo(targetrange);
};