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

How to change the parameters for a task with known task and tasklist ids


I would like use Apps Script to make changes to the status, dates, or note of a task with known id.

I have tried using the following code which shows a change in status when I log the task in the script but the task is not updated in the calendar

task = Tasks.Tasks.get(tasklistID, taskid)

task.setStatus("completed")

Solution

  • In order to modify "the status, dates, or note of a task", how about using the patch method of Tasks API?

    In order to use the sample script, before you run the script, please enable Tasks API at Advanced Google Services and API console. Please confirm it at here.

    Sample script:

    This sample script modifies the status, dates and note of a task of taskId in a list of tasklistId.

    var tasklistId = "###";
    var taskId = "###";
    var resource = {
      status: "completed", // This is either "needsAction" or "completed"
      due: "2019-04-15T00:00:00Z", // Due date of the task (as a RFC 3339 timestamp).
      notes: "sample note",
    }
    var res = Tasks.Tasks.patch(resource, tasklistId, taskId);
    
    • status: Status of the task. This is either "needsAction" or "completed".
    • due: Due date of the task (as a RFC 3339 timestamp). Optional.
    • notes: Notes describing the task. Optional.

    Note:

    • In this sample script, it supposes that you have already known tasklistId and taskId.

    References:

    If I misunderstood your question, I apologize.

    Edit:

    When you want to back the completed task to "needsAction". Please use the following script.

    Sample script:

    var resource = {
      status: "needsAction",
      completed: null,
    };
    Tasks.Tasks.patch(resource, tasklistId, taskId);