How to select checkboxes in Google Docs using Google Apps Script?

360 views Asked by At

I'm new to Google Apps Script and I'm currently working on a project where I need to select all the checkboxes in a Google Doc using Google Apps Script, with the goal of determining if they've been checked or not, I will use this information to populate an external database. However, most of the solutions I've found are related to Google Sheets, not Google Docs.

I've tried using the getListItems() method to retrieve the list items in the document, hoping it would include the checkboxes as well. However, it seems to only select the bulleted points and not the checkboxes. When I run the code, I encounter the error "TypeError: Cannot read properties of null (reading 'toString')" on the line where I try to access the getGlyphType().

Here's the code snippet I've tried:

function getCheckboxValue() {
  const doc = DocumentApp.getActiveDocument();
  const body = doc.getBody();

  const listItems = body.getListItems()
  
  for(let item of listItems){
    console.log(item.getGlyphType().toString())
  }
}

and this is what gets returned

4:47:46 PM  Notice  Execution started
4:47:46 PM  Info    BULLET
4:47:46 PM  Info    BULLET
4:47:46 PM  Info    BULLET
4:47:46 PM  Info    BULLET
4:47:46 PM  Info    BULLET
4:47:47 PM  Error   
TypeError: Cannot read properties of null (reading 'toString')
getCheckboxValue    @ Code.gs:8

I've also included an image of the document layout to provide a visual representation: Sample Doc with Bullets and Checkboxes

Is there a way to select all the checkboxes in a Google Docs document using Google Apps Script? If so, how can I modify my code to achieve this? Any guidance or alternative approaches would be greatly appreciated.

Thank you in advance for your help!

0

There are 0 answers