5
votes

I created a one question form this morning because I wanted to look at the event block for myself. However, the process took an unexpected turn for me. My description follows:

I'm logging the onFormSubmit event with the following code:

function testFormSubmission(e) {
  var lock=LockService.getUserLock();
  try{
      if(lock.tryLock(30000)) {
      var ss=SpreadsheetApp.getActive();
      var sh=ss.getSheetByName('LogSheet');
      var tA=[Utilities.formatDate(new Date(), Session.getScriptTimeZone(),"d/M/yyyy HH:mm:ss")];
      tA=tA.concat(e.values);
      tA.splice(tA.length-1,1,e.triggerUid,e.range.rowStart,e.range.columnEnd,JSON.stringify(e.values));
      sh.appendRow(tA);
      lock.releaseLock();
    }
  }
  catch (e){throw("Couldn\'t get lock for 30 seconds");return;};
}    

I have two images of my spreadsheet below:

There's actually only two columns in e.values one is the date and one is the answer to the question which is either "green" or "blue". The blank columns come from the fact that I started with three questions and collecting emails but decided to remove two of them for simplicity, since I'm generating the submissions myself.

Anyway the responses that don't have either Green or Blue in columnC just shouldn't be there. Column J is simply JSON.stringify(e.values) and it seems to suggest that e has incorrect values in it...I think? Yes/No

Here's an image of the Spreadsheet. (some of it)

This is the Form Responses 1 Sheet:

enter image description here

This is the LogSheet:

enter image description here

So my question is simply where are the unwanted appended lines in the Log Sheet coming from?

I updated my title question because I don't think I'm getting multiple submissions otherwise I'd expect to have multiple lines in Form Responses 1 sheet.

For your information columnH in LogSheet is rowStart so it's easy to figure out what row in Form Response 1 correlates.

2
Hmm I wonder if it's possible the button got clicked twice?jspcal
I restated the question a bit because I don't think I'm getting multiple triggers because the Form Responses 1 sheet doesn't have them..Cooper
@jspcal The button it seems to be pretty well debounced as I can't seem to do a double click on it.Cooper
Cooper, sometimes forms trigger their onsubmit multiple times for a single row of the form. For me at its worst, six times. Add a "return" (to kill the extras) to your catch statement, and open the "executions" for your script, you will see the extra executions. You probably also want to reduce your script lock to a shorter period, maybe 10 seconds. Note- this is a known problem but not a super common one, yet we already had someone here with it today, before that I think it was me two months ago.J. G.
@TheMaster It does appear to work without the lock service. I ran it about 50 times and never got any spurious triggers.Cooper

2 Answers

10
votes

Getting spurious onFormSubmit Triggers

As @J.G. pointed out I was getting more that one trigger from each submission of the form.

I noticed by logging the e.values into a spreadsheet that I was not getting any of the answers. So to eliminate these unwanted triggers I just used the following logic.

if(e.values && !e.values[1]){return;} where e.values[1] was a required question.

My log:

enter image description here

The lines with no value in column C are unwanted triggers.

1
votes

I think your code proved that some triggers are spurious and unwanted and as such Google should not be generating them. I submitted a feedback item to Google on a similar issue.

In my unwanted triggers, I was only getting the e.range.getRow() to determine which row changed, then going to the row to do the required computation. In my case the row contained correct info for all spurious triggers (probably because the first one correctly recorded the data in the spreadsheet).

Your code is a great backstop to prevent it, but I have way too many forms and triggers to cover. Putting this code in all those places would be a pain. Let's hope google solves this issue soon and we don't see any more spurious triggers occurring.