提交,发送电子邮件锁定的Google表单不起作用

时间:2016-05-24 05:50:58

标签: javascript google-apps-script google-sheets google-form

我创建了一个带有电子邮件字段的Google表单。响应存储在电子表格中。在提交时,我从工作表中取最后一行(最新回复)并向该用户发送电子邮件。

我试图实现一个锁系统,但它不能按照我想要的方式工作。如果快速连续提交多个提交(即一分钟内有3个),则只有最后一个用户会收到一封发送给他们的电子邮件,他们实际上会快速连续提交3个重复的电子邮件。

function EmailFormConfirmation() {
// Get a public lock on this script, because we're about to modify a shared resource.
var lock = LockService.getScriptLock()
// Wait for up to 30 seconds for other processes to finish.
lock.waitLock(30000);
 try {
var sheetname = "reg-responses"
var columnnumber = 4

var ss = SpreadsheetApp.getActiveSpreadsheet(); 
var sheet = ss.getSheetByName(sheetname);

// Determines row number of most recent form submission and sets it as "lastrow"
if (sheet.getRange(sheet.getMaxRows(),1).getValue() != "") {
var lastrow = sheet.getMaxRows()
    } 
else {
  var count = 0
  for (var i = 0; i < sheet.getMaxRows(); i++) {
    if (sheet.getRange(sheet.getMaxRows()-i,1).getValue() != "") {
      var lastrow = sheet.getMaxRows()-i
      break;
    }  
  }
 }
  var email = sheet.getRange(lastrow,columnnumber).getValue();                            
  var name = sheet.getRange(lastrow,2).getValue();

  var message = "<HTML><BODY>"
    +"<h3>Your registration has been submitted</h3>"
    +"<p><b>Name</b>: "+name+"</p>"        
    +"</HTML></BODY>";      

  //validation is handled on the form as regex
 MailApp.sendEmail(email, "Form Submitted", "", {htmlBody: message}); 

 //flags it as email sent
 sheet.getRange(lastrow,8,1,1).setValue("Email Sent");

         //Here is the catch if the lock doesn't work
  if(!lock.hasLock()){

     Logger.log('Could not obtain lock after 30 seconds.');
  }
//Secondary catch for the entire function. 
} catch (e) {
Logger.log(e.toString());
}

 // Release the lock so that other processes can continue.
 lock.releaseLock();
}   

我必须解决哪些问题才能解决此问题?

1 个答案:

答案 0 :(得分:2)

您可以检查工作表中的所有行而不是最后一行,如果该行没有将标记设置为“已发送电子邮件”,请发送电子邮件。

这里不是锁定问题,而是检查最后一行的事实。