3

I am trying to make a small Google Script that would automatically add Google Tasks to the "My List" TaskList after searching my GMail emails.

Everything goes fine except for adding a link to the email from which the Task is generated from. Trying to follow the API documentation doesn't really help.

This is the code for the actual task generator function:

function addTask(taskListId, myTitle, myEmailLink) {
  var task = Tasks.newTask(); // effectively same as "= {}".
  task.title = myTitle
  task.notes = 'blank';

  task.links = [{}]
  task.links[0].description = 'Link to corresponding email';
  task.links[0].type = 'email';
  task.links[0].link = 'myEmailLink';

  task = Tasks.Tasks.insert(task, taskListId);
}

Any ideas why the task I receive back has no links?

tehhowch
  • 9,645
  • 4
  • 24
  • 42
borondics
  • 33
  • 3
  • Possible duplicate of [How does Task->setLink work? (php api library)](https://stackoverflow.com/questions/10350321/how-does-task-setlink-work-php-api-library) – tehhowch Apr 29 '18 at 21:15
  • There is an open feature request on the Issue Tracker for `Task#links` to be writable: https://issuetracker.google.com/issues/68982867 – tehhowch Apr 29 '18 at 21:31

2 Answers2

3

As others have noted, according to the Google Tasks API Documentation the links collection is unfortunately read-only.

As a potential work around, it appears you can add links to the notes section of a task, and the links are then directly clickable from the tasks pane in GMail.

Picture: Task with clickable link

Your function can be modified to put the link in the notes section as follows:

function addTask(taskListId, myTitle, myEmailLink) {
  var task = Tasks.newTask(); // effectively same as "= {}".
  task.title = myTitle
  task.notes = 'link: ' + myEmailLink;

  task = Tasks.Tasks.insert(task, taskListId);
}

Combining this with the getPermalink() function on the GmailApp threads object allows for grabbing a deep link to the email you are looking for.

Picture: Task with permalink to email

I'm working on a set of scripts that do some of the things you're talking about in addition to a few other things: https://github.com/tedsteinmann/gmailAutoUpdate

In my solution I have a function that grabs the GMail threads with a particular label (in my case @Task) and then creates a task setting the subject to thread.getFirstMessageSubject() and the notes to thread.getPermalink()

The entire function looks like this:

function processPending_() {

  var label_pending = GmailApp.getUserLabelByName(LABEL_PENDING);
  var label_done = GmailApp.getUserLabelByName(LABEL_DONE);

  // The threads currently assigned to the 'pending' label
  var threads = label_pending.getThreads();

  // Process each one in turn, assuming there's only a single
  // message in each thread
  for (var t in threads) {
    var thread = threads[t];

    // Grab the task data
    var taskTitle = thread.getFirstMessageSubject();
    var taskNote = 'Email: ' + thread.getPermalink();

    // Insert the task
    addTask_(taskTitle, taskNote, getTasklistId_(TASKLIST));

    // Set to 'done' by exchanging labels
    thread.removeLabel(label_pending);
    thread.addLabel(label_done);
  }

  // Increment the processed tasks count
  Logger.log('Processed %s tasks', threads.length);
}
2

Per the Google Tasks API Documentation:

links[] list
Collection of links. This collection is read-only.

You cannot set these links by modifying a Task resource, i.e your code

task.links = [{}]
task.links[0].description = 'Link to corresponding email';
task.links[0].type = 'email';
task.links[0].link = 'myEmailLink';

is simply ignored by the server.

TaskLinks are, to my knowledge, unusable and non-configurable outside of the Googleplex. They may as well not exist to API users.

The only way I've been able to generate a Task that has one is by using the Gmail UI and selecting "Add to Tasks". The resulting task then includes this snippet in the last line of the Task item:

enter image description here

tehhowch
  • 9,645
  • 4
  • 24
  • 42