0
votes

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); 
}
1
Can you show us what you have already tried? Please check this link with help on how to create an example.Robin Gertenbach
We don't know what you know or don't know. Someone would need to start from the beginning and explain everything, or just write the entire code for you. Can you get a script to write anything to a spreadsheet? Anything at all? Are you looking in the Execution Transcript for errors and what line they occurred on? Have you read the Link to TroubleshootingAlan Wells
Welcome to stackoverflow. how to write a good question: stackoverflow.com/help/how-to-askZig Mandel
Thanks for the comments guys! Sorry not use to post question here. I now added the script I found above in my question. Don't know how to modify it tho. Thanks again!spiderzaur

1 Answers

0
votes

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