Google apps script onFormSubmit运行多次

Google apps script onFormSubmit运行多次,google-apps-script,Google Apps Script,我有一个使用谷歌表单设置的维护工作流程。有两个表单提交到同一个电子表格。当提交“请求”表单时,会生成一个随机的6位代码,以及一个预填充的“分配”表单url。然后发送一封包含此信息的电子邮件。然后,收件人可以打开电子邮件,单击预填充的url并分配工作。提交此表格后,此信息将通过电子邮件发送给指定的员工 这一切都可以正常工作,但“onFormSubmit”触发器似乎被多次“触发”。这会导致发送多封电子邮件并更改6位代码 我搜索了stackoverflow,发现一个解决方案是使用“LockServic

我有一个使用谷歌表单设置的维护工作流程。有两个表单提交到同一个电子表格。当提交“请求”表单时,会生成一个随机的6位代码,以及一个预填充的“分配”表单url。然后发送一封包含此信息的电子邮件。然后,收件人可以打开电子邮件,单击预填充的url并分配工作。提交此表格后,此信息将通过电子邮件发送给指定的员工

这一切都可以正常工作,但“onFormSubmit”触发器似乎被多次“触发”。这会导致发送多封电子邮件并更改6位代码

我搜索了stackoverflow,发现一个解决方案是使用“LockService”。然而,这似乎不起作用

我安装了触发器来运行一个“director”脚本,以确定更新了哪个工作表(“request”或“assign”),然后运行相应的代码

我知道我遗漏了一些东西,因为我使用了类似的插件“formMule”,它不会发送多封电子邮件

我能想到的唯一一件事是,我有两张表格提交到同一个电子表格,但我不明白为什么这会是一个问题

请求的代码可以在下面找到

提前感谢您的指导或建议

function director(e){
  var frm = e.range.getSheet().getName();
  switch(frm){
    case "Requests":
      doRequest();
      break;
    case "Assignments":
      doAssign();
      break;
  }
  Logger.log(frm);
}

function doRequest(){
  // Get a script lock, 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);

  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Requests');
  var theRange = ss.getRange(ss.getLastRow(), 1, 1, ss.getLastColumn());
  var theInfo = theRange.getValues();

  theInfo[0][5]=theID();
  theRange.offset(0, 5, 1, 1).setValue(theInfo[0][5]);
  theInfo[0][6] = preFilledURL(theInfo,frmAssignId);
  theRange.offset(0, 6, 1, 1).setValue(theInfo[0][6]);

  var rslt = sendEmail(theInfo);
  if(rslt){Logger.log("Got done with request")};

  lock.releaseLock();
}

/**
 * Creates a pre filled URL 
 *
 * @param {obj} theData Array of the data
 * @param {string} formID The ID of the form
 * @return {string}  Returns the URL
 */

function preFilledURL(theData, formID){

  //get the form and form items
  var form = FormApp.openById(formID)
  var items = form.getItems();

  var resp = form.createResponse();
  resp.withItemResponse(items[0].asTextItem().createResponse(theData[0][5]));
  resp.withItemResponse(items[1].asTextItem().createResponse(theData[0][1]));
  resp.withItemResponse(items[2].asTextItem().createResponse(theData[0][2]));
  resp.withItemResponse(items[3].asParagraphTextItem().createResponse(theData[0][3]));

  return resp.toPrefilledUrl();  
}

function sendEmail(theInfo) {
  var html = "<b>A maintenatnce request has been submitted</b><hr/>Details:<br>Building: " + theInfo[0][1] ;
  html+= "<br>Room or Location: " + theInfo[0][2] + "<br>Issue: " + theInfo[0][3] + "<br>Timestamp: " + theInfo[0][0] + "<br>RCN: " + theInfo[0][5];
  html+= "<br><br><a href=\"" + theInfo[0][6] + "\">Assign Request</a>";

  Logger.log(html);
  GmailApp.sendEmail(recipient, "Maintenance Request - " + theInfo[0][5],"The message",{htmlBody: html});
 // MailApp.sendEmail(recipient, "Maintenance Request - " + theInfo[0][5],"The message",{htmlBody: html});

  return true;
}


/**
* Random string of 6 characters all Caps and Numbers
*
*/
function theID(){
  var randomArray = new Array();
  var chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZ0123456789";
  var string_length = 6;

  var randomstring = '';

  for (var i=0; i<string_length; i++) {
    var rnum = Math.floor(Math.random() * chars.length);
    randomstring += chars.substring(rnum,rnum+1);
  }

  return randomstring;
}  
功能控制器(e){
var frm=e.range.getSheet().getName();
开关(frm){
案例“请求”:
doRequest();
打破
案例“转让”:
doAssign();
打破
}
Logger.log(frm);
}
函数doRequest(){
//获取脚本锁,因为我们将要修改共享资源。
var lock=LockService.getScriptLock();
//等待30秒,等待其他进程完成。
lock.waitLock(30000);
var ss=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Requests');
var-theRange=ss.getRange(ss.getLastRow(),1,1,ss.getLastColumn());
var theInfo=theRange.getValues();
theInfo[0][5]=theID();
范围偏移量(0,5,1,1).setValue(theInfo[0][5]);
theInfo[0][6]=preFilledURL(theInfo,frmAssignId);
范围偏移量(0,6,1,1).setValue(theInfo[0][6]);
var rslt=发送电子邮件(theInfo);
if(rslt){Logger.log(“请求完成”)};
锁。释放锁();
}
/**
*创建预填充的URL
*
*@param{obj}数据的数据数组
*@param{string}formID表单的ID
*@return{string}返回URL
*/
函数preFilledURL(theData,formID){
//获取表单和表单项
var form=FormApp.openById(formID)
var items=form.getItems();
var resp=form.createResponse();
分别为withItemResponse(items[0].asTextItem().createResponse(数据[0][5]);
响应withItemResponse(项[1].asTextItem().createResponse(数据[0][1]);
响应withItemResponse(项[2].asTextItem().createResponse(数据[0][2]);
响应withItemResponse(项[3].AspagraphTextItem().CreateSponse(数据[0][3]);
return resp.toPrefilledUrl();
}
发送电子邮件功能(theInfo){
var html=“已提交维护请求
详细信息:
建筑:“+theInfo[0][1]; html++“
房间或位置:“+theInfo[0][2]+”
问题:“+theInfo[0][3]+”
时间戳:“+theInfo[0][0]+”
RCN:“+theInfo[0][5]; html+=“

”; Logger.log(html); sendmail(收件人,“维护请求-”+theInfo[0][5],“消息”,{htmlBody:html}); //sendmail(收件人,“维护请求-”+theInfo[0][5],“消息”,{htmlBody:html}); 返回true; } /** *6个字符的随机字符串,所有大写字母和数字 * */ 函数theID(){ var randomArray=新数组(); var chars=“abcdefghijklmnopqrstuvwxtz012456789”; 变量字符串长度=6; var randomstring='';
对于(var i=0;我发现同样的事情很有趣。谢谢@Cooper。我会看看这个,看看这是否能解决我的问题。我发现同样的事情很有趣。谢谢@Cooper。我会看看这个,看看这是否能解决我的问题。