Script runs twice on google form submit trigger

2020-07-13 07:50发布

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++; 
   }
}

4条回答
相关推荐>>
2楼-- · 2020-07-13 07:53

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.

查看更多
甜甜的少女心
3楼-- · 2020-07-13 07:58

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...
}
查看更多
不美不萌又怎样
4楼-- · 2020-07-13 08:08

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.

查看更多
▲ chillily
5楼-- · 2020-07-13 08:09

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.

查看更多
登录 后发表回答