dinsdag 24 december 2019

Adding new tasks to a Google Tasks list with Google Apps Script

At work all files are send to a central folder named Postbus. In the folder Postbus the files are renamed and send to a folder into which they belong. Almost all done by GAS-script(s).

The new name of the file of a file contains a task if necessary:
case name-yymmdd-letter in tttyymmdd do something
A GAS-script collect the tasks and (1) send  an email once a week with all the tasks and (2) send an email the day before a task has to be done. Since a week or so a GAS-script adds te tasks also to Google Tasks. I now can see the tasks in Google Calender which is very handy.

function tasksToTasksList(){

var bestanden = DriveApp.searchFiles('title contains "ttt"'); 
  while (files.hasNext()){
     var file = files.next();
     var fileName = file.getName();
     var fileDescription = file.getDescription();
      
     if(/ttt\d\d\d\d\d\d/.test(fileName) && /toTasksList/.test(fileDescription) ==false){
      
        var taskDate = YYMMDD_TO_DATE(/\d\d\d\d\d\d/.exec(fileName.split(/ttt/)[1]));
        var caseName = fileName.split(/-\d{6}-/)[0]; 
        var taskWhat =  fileName.split(/ttt\d{6}/)[1].split(".")[0]; 
        var timeZone = Session.getScriptTimeZone();
        
        if(taskDate.getTime() >= (new Date).getTime()){
        
          var task = {
          title: caseName + " " + taskWhat,
          due: Utilities.formatDate(taskDate,timeZone,"yyyy-MM-dd'T'HH:mm:ss'Z'")
          };
          
          Tasks.Tasks.insert(task,hlptasksToTasksList());
          
          file.setDescription("toTasksList_" + DATE_TO_YYMMDD(new Date()));       } 
     } 
  }
}

function hlptasksToTasksList(){

var lists = Tasks.Tasklists.list().items;

  for(var i=0;i<lists.length;i++){
  
    if(lists[i].title == NAME_TASKSLIST){
    
      var idTasksList = lists[i].id;
      
    }
  }
  return idTasksList;
}

function YYMMDD_TO_DATE(yymmdd){
var dateString = yymmdd.toString(); 
var date = new Date(2000 + Number(datumstring.slice(0,2)),Number(dateString.slice(2,4))-1,Number(datumstring.slice(4,6)));
return date;  
}

function DATE_TO_YYMMDD(date){
var timeZone = Session.getScriptTimeZone();
return Utilities.formatDate(new Date(datum),timeZone, "yyMMdd");
}


Sources:

Geen opmerkingen:

Een reactie posten