Script runs twice on google form submit trigger

泄露秘密 提交于 2020-04-10 18:52:26

问题


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

回答1:


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.




回答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.




回答3:


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.




回答4:


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...
}


来源:https://stackoverflow.com/questions/28449209/script-runs-twice-on-google-form-submit-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!