简体   繁体   中英

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.

I added an email trigger to the code when the clock out function button is clicked from client side. 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.

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. 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.

Follow this answer for how to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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