Question

I have been experimenting with the Google Apps Script Tasks API to create new tasks from emails. I have successfully created TasksList, and Tasks within these lists using both of the following approaches. However, I have been trying to create a child Task by using the optional "parent" resource but with little luck. I am trying to replicate the task "indent" functionality available in Google Tasks which makes a tak appear as a child of the task preceding it. Has anyone managed to get this to work using the API?

Approach 1--

taskListId = getTaskListID("My Projects");
var taskDetails = {
  title: "Task 1",
  notes: "This is a child task",
  parent: "Project A"
};
Tasks.Tasks.insert(taskDetails, tasklistId);

Approach 2 :-

taskListId = getTaskListID("My Projects");
var newTask = Tasks.newTask().setTitle(title);
newTask.setParent("Project A");
Tasks.Tasks.insert(newTask, tasklistId);

in both cases the script creates a task ("Task 1") under a tasklist ("My Projects"), but I cannot get it to appear as a child (indented) task under the existing task ("Project A").

Anyone could shed some light on this?

Was it helpful?

Solution

Actually, @vijay-j is right. You can not set task position directly. You have to set parameters previous and parent while creating a sub-task.

see https://developers.google.com/tasks/reference/rest/v1/tasks/insert
see https://developers.google.com/google-apps/tasks/params

Adding example in Javascript creating a task having 2 sub-tasks:

var taskListId = "add yours here";

var parentTask = Tasks.Tasks.insert({ title: 'Parent' }, taskListId);

var sub1 = Tasks.Tasks.insert(
  { title: 'Sub1' }, 
  taskListId,
  { parent: parentTask.id }
);

var sub2 = Tasks.Tasks.insert( 
  { title: 'Sub2' },
  taskListId,
  { parent: parentTask.id, previous: sub1.id}
);

OTHER TIPS

Ok, I finally found an answer to the above...

I made one fundamental error in the above code, I assumed that the parent setting of a task is the parent task name when in fact it should be the parent task ID allocated to it by the Tasks API.

Then I realised that the parent of a task should be set after it has been inserted into the TasksList and not as part of the initialisation of the new task, so here is what it now looks like:

var taskListID = getTasklistId_(tasklistName);
var parentTask = getParentTask_(projectName,taskListID);
var taskDetails = {
  title: title,
  notes: notes,
  parent: parentTask["id"],
  position: parentTask["idx"]
};
var newTask = Tasks.newTask().setTitle(title);
newTask = Tasks.Tasks.insert(newTask, taskListID, taskDetails);
newTask.setParent(parentTask["id"]);

as you can see I have to reset the parent of the new task. The getParentTask_ function returns the parent-task ID and last child element position to insert the new task

function getParentTask_(taskName, taskListId) {
var tasks = Tasks.Tasks.list(taskListId);
  var taskID;
  var position;
  if (tasks.items) {
    for (var i = 0; i < tasks.items.length; i++) {
      var task = tasks.items[i];
      if(taskName == task.title){ 
        taskID = task.id;
        Logger.log('Task with title "%s" and ID "%s" was found.', task.title, task.id);
      }
      //lets pick up the last child task's position in order to insert the new task below
      if(taskID == task.parent) position = task.position;
    }
  } 
  if( taskID == null){
      var newTask = Tasks.newTask().setTitle(taskName);
      newTask = Tasks.Tasks.insert(newTask,taskListId);
      taskID = newTask.id;
      position = newTask.position;
   }
  var result = {
    id: taskID,
    idx: position
  };
  return result;
}

I hope the above helps someone to be creative with the Google Mail Tasks API.

PS: I used this to link the Wordpress WP Project Manager plugin which creates auto-mailers of new tasks and comments. For the full implementation, you can see this post.

In order to place your task(Child) under some other task(Parent), you should do like this: (The code below is in Java language):

Step 1 : read this documentation carefully: Use Tasks Parameters, Specially read about what is parent and previous.

Step 2 : Now write your code:

//first add parent step 
Task parentTask = service.tasks().insert(taskList.getId(), task). execute();

//now add child task say,session1
Task childTask=new Task();
childTask.setTitle("Session1");
Task response = service.tasks().insert(taskList.getId(), task). execute();

//now move this child task  under  parent task
service.tasks().move(taskList.getId(),response.getId()).setParent(parentTask.getId()).execute();

Hope this will help someone !!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top