简体   繁体   English

如何减少此代码的运行时间?

[英]How reduce the running time for this code?

This is a part of code of employees clock in and clock out from google web app script.这是谷歌 web 应用脚本中员工打卡和打卡代码的一部分。

I added an email trigger to the code when the clock out function button is clicked from client side.当从客户端单击时钟输出 function 按钮时,我在代码中添加了一个 email 触发器。 After I modified this code, the running time has increased which is taking at least 15-60 seconds to execute and due to this long waiting time the employees click multiple time on the clock out button which triggers many emails.在我修改此代码后,运行时间增加了,至少需要 15-60 秒才能执行,并且由于等待时间过长,员工多次单击下班按钮会触发许多电子邮件。

Can you help me to reduce the execution time and possible to stop multiple emails triggers.你能帮我减少执行时间并可能停止多个电子邮件触发器吗?

Thanks in Advance.提前致谢。

     //CLOCK OUT PROCESS
        
        function clockOut() {
          
          
          var user = Session.getActiveUser().getEmail();
          var employee = AdminDirectory.Users.get(user).name.fullName;
          Logger.log('User data:\n%s', JSON.stringify(employee, null, 2));
          console.log(employee);
          
          //DEFINE ALL ACTIVE SHEETS
          var ss = SpreadsheetApp.getActiveSpreadsheet();
          
          //DEFINE MAIN SHEET          
          var mainSheet = ss.getSheetByName("MAIN");
          
          //LAST ROW ON MAIN SHEET
          var lastRow = mainSheet.getLastRow();
          
          var foundRecord = false;
          
          var new_date = new Date();
          var return_date = getDate(new_date);
          var error = 'SUCCESS';
          var return_array = [];
          
          for (var j = 2; j <= lastRow; j++)
          {
            // FIND CLOCK IN RECORD
            if(employee ==  mainSheet.getRange(j, 1).getValue() && mainSheet.getRange(j,3).getValue() == '')
            {
              // UPDATE CLOCK IN RECORD
              mainSheet.getRange(j,3)
              .setValue(new_date)
              .setNumberFormat("dd/MMM/yyyy hh:mm:ss am/pm")
              .setHorizontalAlignment("left")
              .setFontSize(12);
              var totalTime = (mainSheet.getRange(j,3).getValue() - mainSheet.getRange(j,2).getValue()) /(60*60*1000);
              mainSheet.getRange(j,4).setValue(totalTime.toFixed(2))
              .setNumberFormat("#0.00")
              .setHorizontalAlignment("left")
              .setFontSize(12);  
              foundRecord = true;
              
//<-----------------------------------ADDED THIS CODE------------------------->
        //SEND EMAIL AT CLOCK OUT     
        var today_Date = Utilities.formatDate(new Date(), "GMT+1", "EEE dd MMM yyyy");
        var subject = "Your Work Log of The Day - "+today_Date;
        var startTime = mainSheet.getRange(j,2).getDisplayValue();
        
        //If Contion for startTime
        var officeStart = mainSheet.getRange(j,2).getValue();
        if (officeStart.getHours() * 100 + officeStart.getMinutes() >= 0940){
        var startTime = startTime+ "<font color='Red'> ⬤ Late </font>";
        }
        else{
        var startTime =startTime +"<font color='Green'> ⬤ OnTime </font>";
        }
        
        var endTime = mainSheet.getRange(j,3).getDisplayValue();
        var workTime = mainSheet.getRange(j,4).getDisplayValue();
        
        if (workTime > 12){
        var workTime = workTime + "<br> <font color='Red'>LATE RECORD - CONTACT HR </font>";
        }
        else{
        var workTime = workTime;
        }
        var body = "Hi "+employee+"<br><br>Your Total Work Hours - <b>" +workTime +' </b><br><hr width="300" align="left" Color="#bfbfbf" size="0.75">  IN - <b>' + startTime+ '</b> <br><hr width="300" align="left" Color="#bfbfbf" size="0.75"> OUT - <b>'+endTime+"</b>" ;
        GmailApp.sendEmail(user, subject,  body, {htmlBody: body,name: "ABC"});*
            
 //<-----------------------------------END ADDED CODE------------------------->
            }
          }
          
          // IF NO CLOCK IN RECORD
          if(foundRecord == false)
          {
            return_array.push(['Need to Clock In First', '', employee]);
            return return_array; 
          }

The suggestion made by @malarres is a good one. @malarres 提出的建议是一个很好的建议。 The button the users click to clock out should be in some part of the HTML code, with the id of the button, you could use plain JS to disable/enable it.用户单击退出的按钮应该在 HTML 代码的某些部分,使用按钮的 id,您可以使用纯 JS 禁用/启用它。

Follow this answer for how to do it.按照这个答案了解如何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM