0

I have this simple Google Sheets script that adds a row after every already existing one. What do I have to change to for example add 12 rows?

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);

  }

}
1

3 Answers 3

2

There are several ways to change your script. You could use insertRows(nowIndex, numRows) among other methods.

1
  • Thanks for the link. I found there the answer I was looking for. :)
    – Jake St
    Mar 21, 2018 at 14:58
0

My script below helps to add 1 new row above the row 2 but I was thinking to assign a pop up window asking how many rows to add and them run the script based on the number given.

function addRow() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var range = ss.getActiveRange();
  var R = range.getRowIndex();
  
sheet.insertRowsBefore(2,1);
  
}

function onOpen() {
  let ui = SpreadsheetApp. getUi();
  ui.createMenu('Add Row').addItem('Insert','addRow').addToUi();
}
-1
function onEdit(e){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('addnewrow');
  var range = e.range;
  var row = range.getRow();
  sheet.insertRowAfter(row);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.