Google Sheets – How to Extend Formulas and Validation to Newly Added Rows

google sheetsgoogle-apps-script

I have formulas and validation in columns D, E, L, M and N. I want these formulas and validation to be automatic whenever I add rows using the option "Add more rows at the bottom", not using insert or copy & paste.

Sheets example

Is it possible to do it and if so can you please create a code for me?

Best Answer

Edit: fixed version of this: How to automatically insert a new row and retain functions/formulas from last row?

Also copies data validation rules.

Code

// global 
var ss = SpreadsheetApp.getActive();

function onOpen() {
  var menu = [{name:"Add New Last Row", functionName:"addRow"}];
  ss.addMenu("Extra", menu);
}

function addRow() {
  var sh = ss.getActiveSheet(), lRow = sh.getLastRow(); 
  var lCol = sh.getLastColumn(), range = sh.getRange(lRow,1,1,lCol);
  sh.insertRowsAfter(lRow, 1);
  range.copyTo(sh.getRange(lRow+1, 1, 1, lCol), {contentsOnly:false});
}