2

Here is my script:

function CopyRange() {
 var sss = SpreadsheetApp.openById('1EA2F99W1lpgTzX3zoTNpzuEXWh-mDkvo9UyZC4kDNtQ'); //replace with source ID
 var ss = sss.getSheetByName('UKG-A'); //replace with source Sheet tab name
 var range = ss.getRange('G4:K4'); //assign the range you want to copy
 var data = range.getValues();

 var tss = SpreadsheetApp.openById('1EA2F99W1lpgTzX3zoTNpzuEXWh-mDkvo9UyZC4kDNtQ'); //replace with destination ID
 var ts = tss.getSheetByName('UKG-A'); //replace with destination Sheet tab name

 ts.getRange(7 && ts.getLastRow()+1,15,1,5).setValues(data); //you will need to define the size of the copied data see getRange()
 

}

The code works as normal. When I run the code it get data from source sheet and enter on the destination sheet after the last row which has data. But what i want is to define a specific row to start data entry and continue from that row. For example I want data entry to start After the 7th row of UKG-A sheet.

Marios
  • 26,333
  • 8
  • 32
  • 52
Riufaan
  • 43
  • 1
  • 1
  • 5

1 Answers1

1

Explanation:

Your goal is to add a new row after row seven and then set the new data on this newly created row.

  • you can use insertRowAfter(afterPosition) to insert a new row after a specified position.

  • start pasting from row 8.

  • (optional) dynamically get the length of data instead of hardcopying the size.

Solution:

Change:

ts.getRange(7 && ts.getLastRow()+1,15,1,5).setValues(data);

to:

ts.insertRowAfter(7)
ts.getRange(8,15,data.length,data[0].length).setValues(data);

Related:

Marios
  • 26,333
  • 8
  • 32
  • 52
  • Thank you, The code copy the data to row 8. but it keeps over writing the value. what i want is when ever i ran the code it should copy the value to a new row. – Riufaan Feb 15 '21 at 08:52
  • Marios thank you so much for your help. your solution work. But this method create an issue. The issue is i have some data in column A to M in between rows 1 to 33. If i use this method the data in A to M also move down. Is there something like that i can find the last row of column O starting from 6th row and +1 like the original script i have. Thank you. – Riufaan Feb 15 '21 at 19:03
  • @Riufaan thanks for your comment. Could you please post a new question regarding the issue. Stackoverflow does not allow for follow up questions. In this way, future readers will benefit from both questions. Sorry for the inconvenience. – Marios Feb 15 '21 at 19:06
  • Sure. I m really sorry for the inconvenience. – Riufaan Feb 15 '21 at 19:14