Google Scripts onEdit "reacts" two times in a row

50 Views Asked by At

I'm basically trying to keep a blank row on top of a sheet I use for records, the issue here is every time I edit (and I'm making sure I only do it once) one among A5 to J5, it feels like the script runs twice and two rows get added. Plus, newly added rows don't inherit the style and formulas, but I feel like I can fix that.

function onEdit(e) {
  var sheet = e.source.getSheetByName('Transactions');
  var editedRow = e.range.getRow();
  var editedColumn = e.range.getColumn();

  if (editedRow === 5 && editedColumn >= 1 && editedColumn <= 10) {
    sheet.insertRowBefore(5);
  }
}
1

There are 1 best solutions below

0
Cooper On

This should be triggered on any edit of sheet named Transactions and on any column between 1 and 10

function onEdit(e) {
  const sh = e.range.getSheet();
  if (sh.getName() == "Transactions" && e.range.rowStart == 5 && e.range.columnStart < 11) {
    sheet.insertRowBefore(5);
  }
}

Check executions to see if your getting multiple triggers. Your script could fire on edits to other sheets/tabs.