onFormSubmit运行多次

时间:2019-03-11 03:17:12

标签: google-apps-script

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

这一切都有效,但是似乎多次“触发”了“ onFormSubmit”触发器。这将导致发送多封电子邮件并更改6位代码。

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

我已经安装了触发器,以运行“导演”脚本来确定要更新的工作表(“请求”或“分配”),然后运行适当的代码。

我知道我丢失了一些东西,因为我使用了类似的附件'formMule',并且它不会发送多封电子邮件。

我唯一引起问题的原因是我有2个表单提交到同一电子表格,但是我不明白为什么会出现问题。

请求代码可在下面找到。

在此先感谢您的指导或建议。

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

0 个答案:

没有答案