Search code examples
google-apps-scriptgoogle-tasks-apigoogle-tasks

How to create a child task in Google Apps Script Tasks API


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?


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}
    );