Script for Counting Completed Tasks in Google Docs

Hey everyone! I’m trying to figure out how to count finished tasks in my Google Doc. I want to know how many tasks are done out of the total, like “46 out of 103 tasks completed.” Is there a way to create a script for this?

I’ve got a list with checkboxes and I’m a bit stuck because the Google Docs API doesn’t offer a clear method. I saw that there isn’t an isChecked() function and the list attributes don’t indicate whether a checkbox is ticked.

Here’s a sample code snippet I’ve been working on:

function countCheckedBoxes() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var totalItems = 0;
  var checkedItems = 0;

  // Loop through paragraphs
  for (var i = 0; i < body.getNumChildren(); i++) {
    var para = body.getChild(i);
    if (para.getType() == DocumentApp.ElementType.LIST_ITEM) {
      totalItems++;
      // How can I check whether this box is ticked?
    }
  }

  Logger.log(checkedItems + '/' + totalItems + ' tasks completed');
}

Any suggestions on how to detect checked boxes would be greatly appreciated. Thanks!

I encountered a similar challenge and found a workaround. Instead of relying on checkboxes, consider using a specific text marker like ‘’ for completed tasks and ‘’ for incomplete ones. You can then modify your script to search for these markers:

function countMarkedTasks() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var text = body.getText();
var completedTasks = (text.match(//g) || ).length;
var totalTasks = (text.match(/[[ x]]/g) || ).length;

Logger.log(completedTasks + ‘/’ + totalTasks + ’ tasks completed’);
}

This approach is more reliable and doesn’t depend on checkbox functionality. It also allows for easy manual editing if needed.

hey sky24, i had a similar issue. try using getGlyphType() on list items; if it equals DocumentApp.GlyphType.CHECKBOX_CHECKED then the box is ticked.

if (para.getGlyphType() === DocumentApp.GlyphType.CHECKBOX_CHECKED) {
  checkedItems++;
}

hope it helps, lmk if u need more info.

I’ve actually been working on a similar project recently, and I found a neat trick that might help you out. Instead of relying on the checkbox functionality, which can be tricky to work with in Google Docs, I’ve been using custom formatting to track task completion.

Here’s what I do: I use a different text color for completed tasks. Then, my script counts the number of list items with that specific color. It’s been working great for me so far.

Here’s a quick example of how you could modify your script:

function countColoredTasks() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var totalItems = 0;
  var completedItems = 0;

  for (var i = 0; i < body.getNumChildren(); i++) {
    var para = body.getChild(i);
    if (para.getType() == DocumentApp.ElementType.LIST_ITEM) {
      totalItems++;
      if (para.getForegroundColor() == '#00ff00') { // Green color for completed tasks
        completedItems++;
      }
    }
  }

  Logger.log(completedItems + '/' + totalItems + ' tasks completed');
}

This approach gives you more flexibility and control over your task tracking. Plus, it’s visually intuitive when you’re working in the document. Hope this helps!