Search code examples
google-apps-scriptgoogle-sheetstriggersonchange

Google script onchange trigger is firing twice


I am using google script onChange trigger by adding it manually. The sheet is fetching data from an external source and creating new rows as the data is received. So there are 2 events taking place.

  1. A new row is added
  2. Data is written on this new row.

The onChange() trigger is running twice for the same reason but I need to just run it once. Any help would be great. Thanks.


Solution

  • Use the event objects to obtain information about the cause of the trigger firing

    So, for onChange there is e.g. the event object changeType which can contain the values

    • EDIT
    • INSERT_ROW
    • INSERT_COLUMN
    • REMOVE_ROW
    • REMOVE_COLUMN
    • INSERT_GRID
    • REMOVE_GRID
    • FORMAT
    • OTHER

    This can help you to specify that your script shall run only if a certain type of Change takes place.

    Sample:

    function runMeOnIsertRow(e){
      if(e.changeType == 'INSERT_ROW'){
        //do something
      } else{
        return;
      }
    }