Hania
Hania

Reputation: 23

How to make Spreadsheet automatically add new rows?

Upvotes: 2

Views: 3957

Answers (1)

Rodrigo Biffi
Rodrigo Biffi

Reputation: 428

You can try using this function

function insertRow() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('A1').activate();
  spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
  spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
  spreadsheet.getActiveSheet().insertRowsAfter(spreadsheet.getActiveRange().getLastRow(), 1);
  spreadsheet.getActiveRange().offset(spreadsheet.getActiveRange().getNumRows(), 0, 1, spreadsheet.getActiveRange().getNumColumns()).activate();
};

It takes you to the last cell on column A (no matter if it has any data or not, as long as it doesn't have any gaps/empty rows on the way down. If it does, just add extra copies of the getNextDataCell(SpreadsheetApp.Direction.DOWN) code line until it gets you there), and then adds a new row after, so you can call it before running your Importrange().

Also, if you want to add more than one row at a time, just change the last argument in

spreadsheet.getActiveSheet().insertRowsAfter(spreadsheet.getActiveRange().getLastRow(), 1);

Upvotes: 1

Related Questions