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++;
}
}
This is a known issue at Google's end and they are working on a fix:
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:
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.
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.