Need to find a way for data entry to start after a specific row

Viewed 51

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.

1 Answers

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:

Related