6

I'm create Google form and google app script with sendFormByEmail function, also I set on form submit trigger for this function, my issue is this script run two time on form submit and I'm getting two email, I want only single email on form submit. my script code is below.

var no_repeat=0;
function sendFormByEmail(e){

  var email = "test@XXXXDXtest.com"; 

  var s = SpreadsheetApp.getActiveSheet();
  var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];    
  var message = "";
  var subject = "Success Assessment";
  var total=0;
  var roll_on=0;

  message+="test massage";
  message+="<table cellpadding='3' style='color: #0F1F4C;'>";
  for(var i in headers) { 
    if(headers[i]=='Please enter your email address to receive your results'){
      email=e.namedValues[headers[i]].toString();
    }
    if(headers[i]!='Please enter your email address to receive your results'){
      if(headers[i]!='Timestamp'){
        if(e.namedValues[headers[i]]!=''){      
          total = parseInt(e.namedValues[headers[i]])+parseInt(total);
        }

        message +="<tr >";
        message += '<td >'+headers[i]+'</td><td >'+e.namedValues[headers[i]].toString()+ "</td>";
        message +="</tr>";
        roll_on++;
      }
    }
  }  
  message +="<tr >";
  message += "<td ><b> YOUR SCORE </b></td><td ><b>"+total+"</b></td>";
  message+="</tr></table>";

  // Send the email

   if(email!='' && email!=null){
     if(no_repeat==0){ 
       MailApp.sendEmail(email, subject,"",{htmlBody: message});
     }
    no_repeat++; 
   }
}
Wiram Rathod
  • 1,895
  • 1
  • 19
  • 41

5 Answers5

3

This is a known issue at Google's end and they are working on a fix:

Our engineering team is working on the issue, but we don't have any estimates as to when it will be fixed. I advise applying the LockService logic as shown in update #22, which should work around the problem.

Amit Agarwal
  • 10,910
  • 1
  • 32
  • 43
  • hi, amit I'm already gothrouth it befor posting my Questions here, and "lock.waitLock" not working for me I tried it as per solution #22: https://code.google.com/p/google-apps-script-issues/issues/detail?id=4752#c22 – Wiram Rathod Feb 11 '15 at 13:16
  • May be this issue? https://issuetracker.google.com/issues/158892709 link https://issuetracker.google.com/issues/36762960#c26 is not valid for me – Max Makhrov Oct 27 '21 at 08:17
2

I had same issue. The problem was that two users set up the same on submit trigger on the Google Form. I signed in as one of the users and deleted the trigger. I signed in as the other user and the trigger was still there. Works perfectly now, only runs once.

1

I've had the same issue on my spreadsheet, apparently it's a glitch the developers are trying to solve.

I have a similar spreadsheet that runs fine, however it was developed before the last update on the page that manage triggers.

Anyway, as a work around, I've created an extra column on my spreadsheet to ensure the script only runs once for each line, adding two code lines, the first to setvalue to the new column with 'OK' and an if to check that column

Hope it helps!

Att.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
0

Looks like this still hasn't been fixed by the GAS team yet.

After spending weeks trying to get to the bottom of random glitches occurring in our script, we finally found this post. Very frustrating!

We found a simple variable check onSubmit to be an effective workaround:

function handleSubmit() {
    if (window.formSubmitted !== undefined) {
        return false;
    }
    window.formSubmitted = true;
    console.log("Should never fire twice!");

    google.script.run...
}
schmoove
  • 493
  • 1
  • 4
  • 16
0

For a Form Trigger in Google Sheets I've tried this workaround: check the number of row to prevent 2 rows at once:

will also try 'Lock` if this does not work.

My code:

// chech that the row was not affected by trigger earlier

function getSameRowCheck_(row) {
  var c = CacheService.getScriptCache();
  var key = 'somekey'
  var mem = c.get(key);
  if (!mem) {
    return true;
  }
  if (parseInt(mem) >= row) {
    return false;
  }
  c.put(key, row, 21600 );
  return true;
}

Use it in a trigger:

function onFormSubmit(e) {
  var range = e.range;
  var row = range.getRow();
  var check =  getSameRowCheck_(row);
  if (!check) {
    console.log('trigger for the row is running...');
    return;
  }
  // do my job
}
Max Makhrov
  • 17,309
  • 5
  • 55
  • 81