0

I need to have an event fired when a new row on a Google Sheet gets added. I've set up an OnChange event in the IDE and it triggers when I manually insert, delete or even paste in a new row.

The trigger (works for manual changes only):

enter image description here

The onChange function for testing just writes to another tab (sheet) within the spreadsheet, and works fine, for example when making manual changes. BTW I'm not using Logger.log because it's in a doPost function:

function onChange(e) {
  logToSheet("onChange triggered")
}

But it doesn't trigger when writing a new line like this (which does write the row to the sheet):

sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow])

or even when inserting a new line like this:

sheet.insertRowAfter(sheet.getLastRow())

How can I get onChange or another trigger to fire with changes via script as above?

Thank you for any help.

P.S. I've also tried the On Edit trigger but no joy.

MrGreggles
  • 6,113
  • 9
  • 42
  • 48
  • 1
    You can't chain triggers like that. Why not just call `onChange` after `sheet.insertRowAfter(sheet.getLastRow())`? – TheMaster Sep 24 '22 at 09:47
  • "Chained" as in having a trigger based on the actions in doPost, being a trigger itself? The reason for trying to do that is that this processing is being done after the user has clicked Send on the form button. It's currently taking about 8 seconds for the Javascript confirmation Alert to pop up after all this processing completes, which is the reason for wanting to offload writing to the second sheet to make it asynchronous. So just checking if ou're saying that the On Change event won't work from doPost. It seems surprising but I'm a noob so that means nothing. – MrGreggles Sep 24 '22 at 18:36
  • See https://stackoverflow.com/questions/62806366/ – TheMaster Sep 24 '22 at 18:39
  • Yes, that's what I meant. If you see the linked duplicate question(not the one in the comment) and documentation, it's clear only human edits are triggered. The reason for 8s is probably your poor optimization of the script itself. See [tag info page](https://stackoverflow.com/tags/google-apps-script/info) for **"Best practices"** and "FAQ" – TheMaster Sep 24 '22 at 18:49

0 Answers0