提交Google表单,发送电子邮件锁定不起作用 [英] Google Form on Submit, Send Email Lock Doesn't Work

查看:138
本文介绍了提交Google表单,发送电子邮件锁定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

 函数EmailFormConfirmation(){
//获取此脚本的公共锁定,因为我们即将修改共享资源。
var lock = LockService.getScriptLock()
//等待长达30秒的其他进程完成。
lock.waitLock(30000);
尝试{
var sheetname =reg-responses
var columnnumber = 4

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

//确定最近的表单提交的行号并将其设置为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() - 我
休息;
}
}
}
var email = sheet.getRange(lastrow,columnnumber).getValue();
var name = sheet.getRange(lastrow,2).getValue();

var message =< HTML>< BODY>
+< h3>您的注册已被提交< / h3>
+< p>< b>名称< / b>:+ name +< / p>
+< / HTML>< / BODY>;

//验证在表单上作为正则表达式处理
MailApp.sendEmail(email,表单提交,,{htmlBody:message});

//将它标记为发送的电子邮件
sheet.getRange(lastrow,8,1,1).setValue(Email Sent);

//如果锁不起作用,这里就是catch
if(!lock.hasLock()){

Logger.log('Could not 30秒后获得锁定');
}
//整个函数的辅助捕获。
} catch(e){
Logger.log(e.toString());
}

//释放锁定,以便其他进程可以继续。
lock.releaseLock();
}

我必须解决什么问题才能解决此问题?

解决方案

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



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


I have created a google form with an email field. Responses are stored in a spreadsheet. On submit I take the last row from the sheet (latest response) and send an email to that user.

I have tried to implement a lock system, however it does not work the way I would like it too. If a there are multiple submissions in quick succession (i.e 3 within a minute) only the last user will get an email sent to them, they will in fact get 3 duplicate emails the number submitted in quick succession.

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();
}   

What issue must I fix on my lock to resolve this?

解决方案

You can check all the rows in the sheet instead of just the last row and if the row doesn't have the flag set to "Email Sent", send the email.

Not the lock is the problem here, but the fact that you check just the last row.

这篇关于提交Google表单,发送电子邮件锁定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆