Home > Enterprise >  Google Sheets script to paste Range above for n times
Google Sheets script to paste Range above for n times

Time:11-26

I have an issue that I couldn't figure out. I want to copy range A5:G10 with the format and paste above n times. I will put frequency in cell D2. So the script will copy the range and paste above n time(n=Number in cell D2).

Google Sheet Example

I wanted to paste the range n times above, I have a little knowledge of Google Sheet Script, so I don't know how to do this.

CodePudding user response:

Firstly, open a spreadsheet in Google Sheets and then select the cells you want to name. Then, click Data. Named ranges. A menu will open on the right. After that type the range name you want. To change the range, click Spreadsheet .Now select a range in the spreadsheet or type the new range into the text box, then click Ok. Click Done.

CodePudding user response:

Try with this function! I've tested it in your sheet and created a personalized menu called "Copy". Feel free to modify the values to get them as spaced as you may need:

function copyNtimes() {
 var sheet = SpreadsheetApp.getActive().getActiveSheet()
 var range = sheet.getRange("A5:G10") 
 var times = sheet.getRange("D2").getValue()
 sheet.insertRowsAfter(11,7*times)
 SpreadsheetApp.flush()
 for (i=0;i<times;i  ){
   range.copyTo(sheet.getRange(12 7*i,1,6,6)) //you can change that 12 or 7 to determine how much spaced you want the new tables
 }
}
//if you want to create a menu to do it, also use this function:
function onOpen(){
  SpreadsheetApp.getUi().createMenu("Copy")
    .addItem("Copy n times","copyNtimes")
    .addToUi()

}
  • Related