Reputation: 13402
I have a Google Apps Script trigger set up to check for form submissions. However, I would like to check the incoming event values and determine whether the event should actually be added to the spreadsheet or ignored.
Right now I'm doing something like below to remove the last row after its added.
function onFormSubmit(e) {
var sheet = SpreadsheetApp.getActiveSheet();
if (e.values[2] < whatever) {
sheet.deleteRow(sheet.getLastRow());
}
However this approach sometimes fails, I assume because of concurrent events.
Is there a better way to do this ??
Upvotes: 1
Views: 218
Reputation: 24609
How quickly do you need the rows removed? Would it be viable to use a time-driven trigger to clean up the sheet periodically?
Or, using the form submission trigger, you could clear (rather than delete) the rows using e.range, and then use a time-driven trigger to clean out the blank rows.
Or, using the form submission trigger, you could opt to not touch the raw data submissions, but use another sheet (or spreadsheet) to log the accepted e.values using appendRow()
, which appends atomically, getting around the concurrency issue.
Or, you could achieve the same by using a FILTER or QUERY formula in another sheet, but this would probably have the biggest hit on performance.
Upvotes: 3
Reputation: 4693
If it fails because of concurrent events, have a look at the Lock service.
Upvotes: 1