我创建了一个带有电子邮件字段的谷歌表单。响应存储在电子表格中。提交时,我从工作表(最新响应)中获取最后一行并向该用户发送电子邮件。
我尝试实施一个锁定系统,但它也没有按照我想要的方式工作。如果快速连续有多个提交(即一分钟内 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();
}
我必须修复锁上的什么问题才能解决此问题?
您可以检查工作表中的所有行,而不仅仅是最后一行,如果该行没有将标志设置为"电子邮件已发送",请发送电子邮件。
这里的问题不是锁,而是您只检查最后一行的事实。