Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script runs twice on google form submit trigger

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 = "[email protected]"; 

  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++; 
   }
}
like image 704
Wiram Rathod Avatar asked Feb 11 '15 07:02

Wiram Rathod


People also ask

How do I get rid of trigger Google scripts?

Go to script.google.com . At the left, click My Triggers. To delete a trigger, at the right of the trigger, click More more_vert > Delete trigger. Note: Simple triggers like onOpen() can't be deactivated from this page; instead, you must edit the appropriate script and remove or rename the onOpen() function.

Why is my Google script not working?

There are a few possible causes for these errors: A Google server or system is temporarily unavailable. Wait for a few moments and try running the script again. There is an error in your script that doesn't have a corresponding error message.

What is Google App script trigger?

Triggers let Apps Script run a function automatically when a certain event, like opening a document, occurs. Simple triggers are a set of reserved functions built into Apps Script, like the function onOpen(e) , which executes when a user opens a Google Docs, Sheets, Slides, or Forms file.


1 Answers

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.

like image 144
Amit Agarwal Avatar answered Nov 07 '22 18:11

Amit Agarwal