3

I'm attempting to write a Google Script that accomplishes the idea contained in this pseudocode:

access a specific Google tasklist

for each task in that tasklist {
    if the task is completed {
        perform an operation
    }
}

I've run up against a problem -- when my script accesses the specific tasklist, I can only get information for the incomplete tasks, and the completed tasks seem to have disappeared.

The idea seemed easy enough at first. I copied the code from Google's Tasks Service webpage that is supposed to "list the tasks within a given tasklist" (https://developers.google.com/apps-script/advanced/tasks) and added one if statement within the for loop:

function listTasks(taskListId) {
  var tasks = Tasks.Tasks.list(taskListId);
  if (tasks.items) {
    for (var i = 0; i < tasks.items.length; i++) {
      var task = tasks.items[i];

      Logger.log('Task with title "%s" and ID "%s" was found.',
                 task.title, task.id);

      if (task.status == "completed") {
          //perform operation
      }
    }
  } else {
    Logger.log('No tasks found.');
  }
}

When I checked the logs, however, I noticed that the all the incomplete tasks were listed, but none of my completed tasks were listed. I tried running the script on a tasklist with only completed tasks and the log output was "No tasks found."

Does anyone know how to find completed tasks in a given tasklist? It seems that this should be possible, based on the fact that Zapier performs operations for completed tasks, as described in this other post: How to trigger Google Script when a Google Task is marked "completed"

anothermh
  • 9,815
  • 3
  • 33
  • 52
  • 1
    Whoops. I didn't find any articles on stackoverflow when I used the search bar, but I just checked the google-tasks-api tag page, and it looks like someone had the same issue, and it has been reported to Google: https://stackoverflow.com/questions/52179087/google-tasks-api-does-not-give-update-about-task-completion-when-new-gmail-theme . If anyone knows anything further, let us know, but it looks like it might just be a bug. – Tobias Letak Oct 18 '18 at 03:51

3 Answers3

2

You need to tell the Task API that you want List Tasks to return the completed tasks as well.

I used PHP, and you have to tell the Google Service that you want 'hidden' tasks. It seems that completed == hidden. If you want deleted then you can also send that parameter.

$optParams = array(
    'showHidden' => true,
    'showDeleted' => true,
);

$GTresults = $service->tasks->listTasks($tasklist, $optParams);

Edited here: Added an example of a loop based on completed:

$retrievedTask = $service->tasks->get($tasklist, $taskID);

if($retrievedTask->getStatus() == "completed") {
    echo "GT uncompleted", "\n";
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Jon
  • 21
  • 4
2

Just send optional params into function:

var optionalArgs = {
    maxResults: 100,
    showHidden: true,
    showDeleted: true
  };
var tasks = Tasks.Tasks.list(taskListId, optionalArgs);
Miroslav
  • 31
  • 4
0

I had same problem. And I found that I can get completed tasks by the API if I complete the task by old google task UI (https://mail.google.com/tasks/canvas).

How ever I couldn't get those if I complete the task by new gmail UI. As you pointed out at the comment, this problem may caused by new gmail UI.