问题
I have a script that creates additional files upon submission of a Google Form. It also sets up a trigger for said new document, so that people are emailed when any edits are made to the document after its creation, e.g.
ScriptApp.newTrigger("sendEmailOnModification")
.forSpreadsheet(SpreadsheetApp.openById(fileId))
.onEdit()
.create();
The logic all works, but... the trigger is extremely sensitive. Every time someone makes a single keystroke in the document, it fires. It doesn't make a lot of sense to repeatedly fire as someone writes a paragraph; it makes more sense to, say, fire 10 minutes after the last edit. (Or something. Anything that doesn't spam emails.)
Is there a way to modify the trigger so that it fires less often (I do not want a time-based trigger), or otherwise programmatically change how frequently Google Sheets saves?
As a last resort, I suppose I could create a hidden sheet that captures last modified time and a condition not to send an email if subsequent modifications are less than X minutes ago, but that's kind of clunky and not really appealing. Hoping I can do it entirely in Google Scripts instead.
回答1:
You could use the Properties Service to save a timestamp of the last time that the email was sent and compare it to the actual time of the current execution.
回答2:
I'm not sure how you could implement this to suit your needs,but since you don't want a time based trigger, and you are already using onEdit()
, you should be able to use
onEdit(e)
and e.oldValue
to check if the change is worth the message.
来源:https://stackoverflow.com/questions/53752122/how-can-i-modify-a-trigger-so-that-it-emails-upon-edit-but-not-so-quickly