Abhijeet Singh
Abhijeet Singh

Reputation: 13

Adding specific number of rows in google sheets

I want to add 11 number of rows after every row. the code which I have made adds only 1 row. I don't know how to add 11 rows to be exact.

function addRows(){
  var sheet = SpreadsheetApp.getActiveSheet();
  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();

  for (var i = 1; i <= numRows*2 - 1; i+=2) {
   sheet.insertRowAfter(i);
  }
}

Upvotes: 1

Views: 157

Answers (1)

Iamblichus
Iamblichus

Reputation: 19309

  • You can specify the number of rows to add by using insertRowsAfter.
  • To avoid the added rows messing up with your loop, you can insert rows in the reverse order. It could be something like this:
function addRows(){
  var sheet = SpreadsheetApp.getActiveSheet();
  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  for (var i = numRows; i > 0; i--) {
    sheet.insertRowsAfter(i, 11);
  }
}

I hope this is of any help.

Upvotes: 1

Related Questions