Google-sheets – How to trigger onChange trigger on inserts only

google sheetsgoogle-apps-scriptgoogle-apps-script-triggers

I am trying to create a trigger that sends emails when lines are appended or inserted to a sheet. The problem I have is that the python script that is connected to the google sheet purges sheet "new_strikes" then inserts new information in the same Google sheet. I have a onChange trigger like so:

function trigFunc(e){
  var activeSheet = e.source.getActiveSheet();
  if(activeSheet.getName() != 'new_strikes') return;
  Utilities.sleep(30);
  sendEmail();
}

The problem is when I purge then insert data into this sheet, the script runs twice–once for the purge and once for the edit. Since the trigger sends an email, I get two emails. How do I modify the code or trigger so that it only tracks the insertion of data and therefore sends one email?

Best Answer

Instead of calling trigFunc(e) directly from the on change trigger, add the following function to your project and set your trigger to call it:

function respondToOnChange(e){
  if(e.changeType === 'INSERT_ROW') trigFunc(e);
}

Resources