允许在相关日期将任务添加到清单的脚本

时间:2019-01-12 17:19:40

标签: javascript date google-apps-script google-sheets task

我有一个脚本,它将使用任务列表查看检查频率(每天,每周,每年两次,时钟更改(夏令时)),并在这些检查中填充日志(如果检查即将完成)

当前它适用于每日和每周任务,因为那只是比较两个日期并添加1,7,但是我需要能够在时钟发生变化之前将其添加到日志中,以便相关团队可以完成检查。

我相信主要的问题是我只是在比较日期上的差异。

这是我当前的代码...如果没有意义,请问!

function fillTasksLog(){

var ss = SpreadsheetApp.getActiveSpreadsheet();

var Settingss = ss.getSheetByName('Settings')
var teamList = Settingss.getRange("A2:C").getValues();

for(var team in teamList){
  if(teamList[team][0] != ""){
    var WMsheet = ss.getSheetByName(teamList[team][0])
    var WMSheetLog = ss.getSheetByName('Log')
    var taskList = WMsheet.getRange("A4:I").getValues();
    var taskLog = WMSheetLog.getRange("A4:Q").getValues();

    var nextFreeRow = WMSheetLog.getLastRow()+1
    var count = 0;
      for (var task in taskList){
        var latestDate = new Date("2017-01-21T20:23:26+0000"); 
        var existingTaskFound = false;
        for(var loggedTask in taskLog){
           var uniqueLogID = taskLog[loggedTask][13];
           var uniqueTaskListID = taskList[task][8];
           var uniqueLogDate = taskLog[loggedTask][9];
           var checkFrequencyDays = 0;

           switch(taskList[task][0]){// looks for date type (frequency)of item in log and assigns value
           //ADD WEEKDAYS TO SWITCH
                case "Daily" :
                  //Logger.log("INSIDE SWITCH DAILY: ");
                  checkFrequencyDays=1;
                break;
                case "Weekly" :
                  //Logger.log("INSIDE SWITCH WEEKLY: ");
                  checkFrequencyDays=7;
                break;
                case "Monthly" :
                  //Logger.log("INSIDE SWITCH Monthly: ");
                  checkFrequencyDays=30.5;
                break;
                case "Quarterly" :
                  //Logger.log("INSIDE SWITCH QUARTERLY: ");
                  checkFrequencyDays=91.25;
                break;
                case "Bi-annually" :
                  //Logger.log("INSIDE SWITCH BI-ANNUALLY: ");
                  checkFrequencyDays=182.5;
                break;
                case "Annually" :
                  //Logger.log("INSIDE SWITCH ANNUALLY: ");
                  checkFrequencyDays=365;
                break;
                default :
                  //Logger.log("INSIDE SWITCH DEFAULT: ");
                  checkFrequencyDays=1;
                break;
              }
             if(uniqueLogID == uniqueTaskListID){
                existingTaskFound = true;
                //Logger.log("checkFrequencyDays: "+checkFrequencyDays);
                //Logger.log("LATEST"+latestDate);
                //Logger.log(taskLog[loggedTask][16]);

                if(latestDate < taskLog[loggedTask][14]){
                  latestDate = taskLog[loggedTask][14];
                  if(taskLog[loggedTask][10] == ""){
                    WMSheetLog.getRange('K' + (parseInt(loggedTask) + 4)).setValue("No");
                  }
                }
             }
        }
        if(existingTaskFound){
          var dt1 = new Date(); // today's date
          var dt2 = latestDate;

          // get milliseconds
          var t1 = dt1.getTime();
          var t2 = dt2.getTime();

          var diffInDays = Math.floor((t1-t2)/(24*3600*1000));
          // 24*3600*1000 is milliseconds in a day
          //Logger.log("DIFFINDAYS: "+diffInDays);
          if(diffInDays >= checkFrequencyDays){
            addTaskToLog(WMSheetLog, nextFreeRow, count,taskList, task, teamList[team][0], teamList[team][1]);
            count ++;
          }
        }else{
          addTaskToLog(WMSheetLog, nextFreeRow, count,taskList, task, teamList[team][0], teamList[team][1]);
          count ++;
        }

      }
    }
  }
}

function addTaskToLog(WMSheetLog, nextFreeRow, count,taskList, task, teamName, teamType){
  var todaysDate = new Date();
  WMSheetLog.getRange('A' + (nextFreeRow + count)).setValue(taskList[task][0]);
  WMSheetLog.getRange('B' + (nextFreeRow + count)).setValue(taskList[task][1]);
  WMSheetLog.getRange('C' + (nextFreeRow + count)).setValue(taskList[task][2]);
  WMSheetLog.getRange('D' + (nextFreeRow + count)).setValue(taskList[task][3]);
  WMSheetLog.getRange('E' + (nextFreeRow + count)).setValue(taskList[task][4]);
  WMSheetLog.getRange('F' + (nextFreeRow + count)).setValue(taskList[task][5]);
  WMSheetLog.getRange('G' + (nextFreeRow + count)).setValue(taskList[task][6]);
  WMSheetLog.getRange('H' + (nextFreeRow + count)).setValue(taskList[task][7]);
  WMSheetLog.getRange('N' + (nextFreeRow + count)).setValue(taskList[task][8]);
  WMSheetLog.getRange('O' + (nextFreeRow + count)).setValue(todaysDate);
  WMSheetLog.getRange('P' + (nextFreeRow + count)).setValue(teamName);
  WMSheetLog.getRange('Q' + (nextFreeRow + count)).setValue(teamType);
}

更新的问题1月13日:

预期结果是,如果某人有一项必须在时钟更改之前完成的任务(甚至可能要在两周之前完成),那么该任务将在相关日期添加到日志中。例如,今年的时钟在3月31日(星期日)更改...我希望将此任务最多在两周前添加到日志中,以便团队知道他们需要完成一项任务。目前,脚本尚不知道时钟的更改日期,只是指一个简单的计算,即每6个月等于182.5天

每半年执行一次任务就一样

1月14日更新图片:

Page for team to update their task list(Sheet entitled team name)

Settings page with team information

Main log populated by each teams task list

我需要的另一个功能是,如果脚本可以理解的话,是否需要在某天进行检查。

0 个答案:

没有答案