Google Sheets macro to copy a line of data to next empty row on another sheet - Stack Overflow

I'm new to google sheets and macros and need a little help please. I'm trying to copy a range

I'm new to google sheets and macros and need a little help please. I'm trying to copy a range of data (row) from A4:H4 on the 'Front Sheet' tab to the next empty row on the 'Data' tab.

I have recorded the macro and have tried modifying it to add in some code to put it in the last row but I'm not really sure how to do this. I'd be really grateful for any help please.

This is my recorded (basic) code:

function Copydata() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A4:H4').activate();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Data'), true);
spreadsheet.getRange('A3').activate();
spreadsheet.getRange('\'Front Sheet\'!A4:H4').copyTo(spreadsheet.getActiveRange(),
SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false); 
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Front Sheet'), true);
spreadsheet.getRange('A7').activate();
};

I'm new to google sheets and macros and need a little help please. I'm trying to copy a range of data (row) from A4:H4 on the 'Front Sheet' tab to the next empty row on the 'Data' tab.

I have recorded the macro and have tried modifying it to add in some code to put it in the last row but I'm not really sure how to do this. I'd be really grateful for any help please.

This is my recorded (basic) code:

function Copydata() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A4:H4').activate();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Data'), true);
spreadsheet.getRange('A3').activate();
spreadsheet.getRange('\'Front Sheet\'!A4:H4').copyTo(spreadsheet.getActiveRange(),
SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false); 
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Front Sheet'), true);
spreadsheet.getRange('A7').activate();
};
Share Improve this question edited Nov 20, 2024 at 12:40 Wicket 38.5k9 gold badges78 silver badges193 bronze badges asked Nov 20, 2024 at 12:28 Jay BJay B 33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Getting the Last Row of a Targeted Range

Basically, what you want to utilize here is a Method called getLastRow().

I understand that you are new with Google Apps Script, I do hope that you enjoy using the platform. Instead of correcting your code all the way, I just intended to create a new one that caters to your needs. You might want to review, active(), getRange(), and copyTo(). This function is close to what you want to achieve I do think that it will introduce unneccesary runtime which is very important when working with Google Apps Script.

I created a simple solution that already do what you are looking forward to do and you can use as a basis for your future projects.

Sample Code:

function copySheettoAnotherSheet() {
  var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Front Tab"); // Change the Sheet Name Depending on your situation
  var targetSheet =  SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data"); // Change the Sheet Name Depending on your situation
  var copyRangeValues = sourceSheet.getRange().getValues(); // This is the part of getting the Values from your desired range. 
  var pasteRange = targetSheet.getRange(targetSheet.getLastRow()+1,1,copyRangeValues.length,copyRangeValues[0].length); // This is getting a range, that will make sure that you are on the right size of the data you are trying to paste.
  pasteRange.setValues(copyRangeValues) // the pasting of Data
}

References:

getLastRow()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742357049a4428644.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信