多次执行Google脚本,即使在表单提交时锁定提交触发器

时间:2019-02-22 18:16:36

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

我很难理解为什么每次提交表单时该脚本都同时执行2、3甚至4次。我已经添加了LockService,但是虽然有时确实可以阻止发送多封电子邮件,但它似乎并不能阻止同时执行。

一切正常-我只是不想每次都执行失败,因为由于脚本超时,这些操作还会破坏后续的合法触发器执行。

我必须从工作表中执行,因为我正在调用基于表单响应检索到工作表的数据。工作表未共享,也没有“孤立的”触发器。我已经删除/重新创建了触发器,甚至重新创建了工作表和脚本,但仍然获得了额外的执行力。

提前感谢您的任何建议!

function zoneRepEmail(e) {

  var lock = LockService.getScriptLock();
  lock.waitLock(30000);  // wait 30 seconds before conceding defeat.
  // got the lock, you may now proceed
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('SupervisorApprovals');
  var lookupRow = e.range.getRow(); 
  var rowRange = sheet.getRange(1,1,sheet.getLastRow(),37);
  
  var appEmailSentLog = rowRange.getCell(lookupRow,36);
  var repEmailSentLog = rowRange.getCell(lookupRow,37); 
   Logger.log(appEmailSentLog);
  
  var form = FormApp.openById('1efkzyAgHf3PjEnPXSXP4P5mTCnII_BQaf5jf2Apr8gM');
   
  var startRow = 3;
  var headers = sheet.getRange(2, 1, 1, sheet.getLastColumn()).getValues()[0];
 
  var timestampColumn = sheet.getRange('K'+lookupRow);
  var dateColumns = sheet.getRangeList(['X'+lookupRow, 'Y'+lookupRow]);
  var pctColumns = sheet.getRange('Z'+lookupRow);
  var phoneColumns = sheet.getRangeList(['P'+lookupRow,'Q'+lookupRow,'U'+lookupRow]);
  var dataRange = sheet.getRange(lookupRow,1,sheet.getLastRow(),37);
  var data = dataRange.getValues();
    
    
  for (var i = 0; i < data.length; ++i){
      var row = data[i]; 
      var timestamp = e.namedValues["Timestamp"];
      var columnOfEditUrl = 46;
      var email_address = "email@email.com";
      var columnOfHomeUnit = 44;
      var columnOfAppId = 45;
      var columnOfZoneRep = 47;
      var columnOfZoneRepEmail = 48;
      var colEmplLName = 2;
      var emplLName = sheet.getRange(lookupRow, 12).getValue();
      var colEmplFName = 3;
      var emplFName = sheet.getRange(lookupRow, 13).getValue();
      var emplMName = sheet.getRange(lookupRow, 14).getValue();
      var locDisp = sheet.getRange(lookupRow, 15).getValue();
      var empPhone = row[15];
      var emplAltPhone = row[16];
      var emplEmail = row[17];
      var emplClass = row[18];
      var emplSup = row[19];
      var supPhone = row[20];
      var supEmail = row[21];
      var trainPosition = row[22];
      var firstAssign = row[23];
      var lastAssign = row[24];
      var tbPercent = row[25];
      var trainIMT = row[26];
      var imtName = row[27];
      var reqTraining = row[28];
      var trainNeeded = row[29];
      var appComments = row[30];
      var homeUnit = row[31];
      var appId = row[1];
      var zoneRep = row[33];
      var zoneRepEmail = sheet.getRange(lookupRow, 35).getValue();
      var appStatus = row[6];
      var statusReason = row[7];
      var statusComment = row[8];
      
    timestampColumn.setNumberFormat("mm/dd/yyyy hh:mm:ss");
    pctColumns.setNumberFormat("###%");
    dateColumns.setNumberFormat("mm/dd/yyyy");
    phoneColumns.setNumberFormat("###-###-####");
    
      var emailTo = emplEmail;
      var message = "The following application to the Rocky Mountain Area Priority Trainee Program has been "+appStatus+" for "+emplFName+" "+emplMName+" "+emplLName+". Please carefully review all details in the application and update your zone sheet accordingly."+"\n"+"If corrections are required, please contact the applicant directly."+"\n"+"A status email has been forwarded to the applicant and supervisor listed."+"\n"+"\n"+"--------------------------------------------------------------"+"\n"+"\n"+"Applicant Name: "+emplFName+" "+emplMName+" "+emplLName+"\n"+"Application Reference ID: "+appId+"\n"+"Dispatch: "+locDisp+"\n"+"Employee Phone: "+empPhone+"\n"+"Employee Alternate Phone: "+emplAltPhone+"\n"+"Employee Email: "+emplEmail+"\n"+"Home Unit ID: "+homeUnit+"\n"+"Employment Class: "+emplClass+"\n"+"\n"+"Supervisor Name: "+emplSup+"\n"+"Supervisor Phone: "+supPhone+"\n"+"Supervisor Email: "+supEmail+"\n"+"\n"+"Trainee Position: "+trainPosition+"\n"+"Taskbook Percentage Complete: "+tbPercent+"\n"+"Taskbook First Assignment: "+firstAssign+"\n"+"\n"+"IMT Trainee: "+trainIMT+"\n"+"Assigned to IMT: "+imtName+"\n"+"\n"+"All Required Training Completed?: "+reqTraining+"\n"+"Training Still Needed for Position: "+trainNeeded+"\n"+"\n"+"Comments: "+appComments+"\n"+"\n"+"Application Status: "+appStatus+"\n"+"Reason for Denial (if applicable): "+statusReason+"\n"+"Supervisor Comments: "+statusComment;
      var appMessage = "The following application to the Rocky Mountain Area Priority Trainee Program has been "+appStatus+" for "+emplFName+" "+emplMName+" "+emplLName+". The application has been forwarded to the Zone Training Representative for processing."+"\n"+"\n"+"--------------------------------------------------------------"+"\n"+"\n"+"Applicant Name: "+emplFName+" "+emplMName+" "+emplLName+"\n"+"Application Reference ID: "+appId+"\n"+"Dispatch: "+locDisp+"\n"+"Employee Phone: "+empPhone+"\n"+"Employee Alternate Phone: "+emplAltPhone+"\n"+"Employee Email: "+emplEmail+"\n"+"Home Unit ID: "+homeUnit+"\n"+"Employment Class: "+emplClass+"\n"+"\n"+"Supervisor Name: "+emplSup+"\n"+"Supervisor Phone: "+supPhone+"\n"+"Supervisor Email: "+supEmail+"\n"+"\n"+"Trainee Position: "+trainPosition+"\n"+"Taskbook Percentage Complete: "+tbPercent+"\n"+"Taskbook First Assignment: "+firstAssign+"\n"+"\n"+"IMT Trainee: "+trainIMT+"\n"+"Assigned to IMT: "+imtName+"\n"+"\n"+"All Required Training Completed?: "+reqTraining+"\n"+"Training Still Needed for Position: "+trainNeeded+"\n"+"\n"+"Comments: "+appComments+"\n"+"\n"+"Application Status: "+appStatus+"\n"+"Reason for Denial (if applicable): "+statusReason+"\n"+"Supervisor Comments: "+statusComment;
      var subject = appStatus+": PT App for "+emplFName+" "+emplMName+" "+emplLName+" // "+trainPosition+" // "+timestamp;

       
    Utilities.sleep(5000);//allow all column formats to be applied, then proceed.
    
 if (repEmailSentLog.isBlank()){
   GmailApp.sendEmail(zoneRepEmail, subject, message,{noReply:true});
    repEmailSentLog.setValue('EmailSent');
 }
    if (appEmailSentLog.isBlank()){
      GmailApp.sendEmail(emplEmail, subject, appMessage,{cc:supEmail,noReply:true});
    appEmailSentLog.setValue('EmailSent');
    }
    
  }
   lock.releaseLock(); Utilities.sleep(10000);
}

1 个答案:

答案 0 :(得分:1)

我一定会删除该行:

:existing
echo Welcome to Androids: Became Human!
echo ==================================
echo.
echo It appears that you already created
echo configuration file. Would you like
echo to re-create it?
echo.
echo Please enter (Y,N):
set /p choice=
if %choice% == "y" goto welcome
if %choice% == "n" exit
if %choice% == "Y" goto recreation
if %choice% == "N" exit

没有理由等待。

如果要避免由Apps脚本限制引起的速率限制错误,则将对服务的调用置于Utilities.sleep(10000); 循环内,并且仅在出现错误时等待。这样,如果不需要,代码就不会等待,只有需要时才等待。

我会增加超时时间。通常情况下,代码会释放该锁,因此长时间的超时不会影响这种情况,但是如果超时在代码完成之前结束,则可能会导致问题。

如果有错误,请释放锁。因此,以下代码添加了一个try / catch块来处理该问题。

此外,您应该在继续操作之前测试获得的锁,如果未获得该锁,请退出并通知某人错误。

for