2
votes

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"

3
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: stackoverflow.com/questions/52179087/… . If anyone knows anything further, let us know, but it looks like it might just be a bug. - Tobias Letak

3 Answers

1
votes

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

I found the answer here: https://developers.google.com/tasks/v1/reference/tasks

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

I hope that helps.

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";
}
1
votes

Just send optional params into function:

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

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.