0

I want Google apps script that should read the google tasks completed and add them as a row to google spreadsheet.. Can anyone help me

droidBomb
  • 850
  • 5
  • 8
  • Please [edit](http://stackoverflow.com/posts/43463046/edit) the question to be on-topic: include a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) that duplicates the problem. Questions seeking debugging help ("why isn't this code working the way I want?") must include: (1) the desired behavior, (2) a specific problem or error and (3) the shortest code necessary to reproduce it in the question itself. Please also see: [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), and [How to Ask](http://stackoverflow.com/help/how-to-ask) – abielita Oct 15 '18 at 13:59

1 Answers1

1

Read more https://developers.google.com/apps-script/advanced/tasks

Is need to apply optional argument showHidden = true for getting completed tasks.

In script editor open menu Resources - Advanced Google Services and switch on "Tasks API". Then try this code:

function myFunction() {
  getCompletedTasks(getTaskListByTitle('ToDo'));
}

function getTaskListByTitle(titleTasksList) {
  if (typeof titleTasksList === 'undefined') { titleTasksList = 'default'; }
  var rezultId = 0;
  var response = Tasks.Tasklists.list();
  var taskLists = response.items;
  if (taskLists && taskLists.length > 0) {
    for (var i = 0; i < taskLists.length; i++) {
      var taskList = taskLists[i];
      if (titleTasksList == 'default') {
        rezultId = taskList.id; //return first item
        break;
      } else {
        //Logger.log('%s (%s)', taskList.title, taskList.id);
        if (titleTasksList == taskList.title) {
          rezultId = taskList.id;
          break;
        }
      }
    }
  } else {
    Logger.log('No task lists found.');
  }
  return rezultId;
}

function getCompletedTasks(taskListId) {
  var optionalArgs = {
    maxResults: 100,
    showHidden: true  
  };
  var tasks = Tasks.Tasks.list(taskListId, optionalArgs);
  var SPREADSHEET = SpreadsheetApp.getActiveSpreadsheet();
  var rngStartReport = SPREADSHEET.getRange('A1');
  var k = 0;
  if (tasks.items) {
    for (var i = 0; i < tasks.items.length; i++) {
      var task = tasks.items[i];
      rngStartReport.offset(k, 0).setValue(task.title);
      rngStartReport.offset(k, 1).setValue(task.status); 
      k++;
      Logger.log('Task with title "%s" and ID "%s" was found.',
                 task.title, task.id);
    }
  } else {
    Logger.log('No tasks found.');
  }
}
Miroslav
  • 31
  • 4