Jaden Ranzenberger
Jaden Ranzenberger

Reputation: 69

DocumentApp for Google Apps Script not returning all attributes of an element?

When I attempt to get attributes from an element using the getAttributes() method from the Google Apps Script DocumentApp library for a Google Doc, not all attributes show up; specifically the GLYPH_TYPE attribute for a listItem element.

Due to the lack of functionality within Google Docs for checklists, I'm writing a code script to automatically calculate the percent completion of a list based on the symbol present of each list item belonging to a list. In order to do this, I am retrieving all listItems relevant to a listID, and then attempting to check their GLYPH_TYPE attribute in order to differentiate which items have been completed (which the user indicates by clicking the symbol left of the list item and changing it to a checkmark). It is the latter that I am having issues with; when I call getAttributes() for either the Paragraph or the listItem object for the list item, GLYPH_TYPE isn't present at all.

Here's a method where I am simply trying to extract the GLYPH_TYPE attribute for a listItem;

function getGlyphTypeFromListItem(documentId){
  var doc = 
  DocumentApp.openById("1mzVQUccSH_suf8KoTkbVN4XjIOcfbYDuie3GV_M1Fg8");
  var body = doc.getBody();
  Logger.log(body.getParagraphs()[0].getAttributes());
  Logger.log(body.getParagraphs() 
  [0].findElement(DocumentApp.ElementType.LIST_ITEM).getElement()
  .getAttributes()); 
}

When I run this method I get the responses:

[19-01-20 18:22:02:242 MST] {FONT_SIZE=11, ITALIC=true, HORIZONTAL_ALIGNMENT=null, INDENT_END=null, INDENT_START=144.0,LINE_SPACING=null, LINK_URL=null, UNDERLINE=true, 
BACKGROUND_COLOR=null, INDENT_FIRST_LINE=126.0, LEFT_TO_RIGHT=true, 
SPACING_BEFORE=null, HEADING=Normal, SPACING_AFTER=null, 
STRIKETHROUGH=null, FOREGROUND_COLOR=null, BOLD=true, 
FONT_FAMILY=Roboto Condensed}
[19-01-20 18:30:21:253 MST] {FONT_SIZE=11, ITALIC=true, 
HORIZONTAL_ALIGNMENT=null, INDENT_END=null, INDENT_START=144.0, 
LINE_SPACING=null, LINK_URL=null, UNDERLINE=true, BACKGROUND_COLOR=null, 
INDENT_FIRST_LINE=126.0, LEFT_TO_RIGHT=true, SPACING_BEFORE=null, 
HEADING=Normal, SPACING_AFTER=null, STRIKETHROUGH=null, FOREGROUND_COLOR=null, 
BOLD=true, FONT_FAMILY=Roboto Condensed}

As you can see, GLYPH_TYPE is completely absent from both of the logs; is there something I'm missing? Also, have any of you found a more intuitive way to track the completion of "checklists" within Google Docs?

Thank you in advance!

Upvotes: 1

Views: 1501

Answers (2)

Tedinoz
Tedinoz

Reputation: 7959

Google documentation says that the "Paragraph" getAtributes method (doc) "retrieves the element's attributes. The result is an object containing a property for each valid element attribute where each property name corresponds to an item in the DocumentApp.Attribute enumeration." GLYPH_TYPE is recorded as one of the properties of "Enum Attribute".

I agree with the OP that the GLYPH TYPE is not reported when a paragraph is analysed. However if the Paragraph Type = DocumentApp.ElementType.LIST_ITEM, then getAttributes does report the GLYPH_TYPE, as well as some additional data (Refer image below). It is difficult to say whether there is a bug in the Google code, or (perhaps) an error in the documentation.

So far as the OP code is concerned, I got the same results.

But it's interesting to note a ListItem is returned by getParagraphs but a ListItem is NOT a 'paragraph'. ("A Paragraph may contain Equation, Footnote, HorizontalRule, InlineDrawing, InlineImage, PageBreak, and Text elements." (docs)). The getParagraphs method "retrieves all the Paragraphs contained in the section (including ListItems)" (docs). It's an odd, almost contradictory result, but I think this may be a reason why the OP's code was unsuccessful.


This was my code (adapted from the OP).

function so54282539() {

  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();

  // variable to count the list#
  var listcounter = 0;

  // get the Paragraphs
  var paras = doc.getParagraphs();
  //Logger.log("DEBUG: paras = "+paras);//DEBUG
  Logger.log("DEBUG:Number of paragraphs: " + paras.length); //DEBUG

  // get the List Items
  var lists = doc.getListItems();
  //Logger.log("DEBUG: Lists: "+lists);//DEBUG
  Logger.log("DEBUG: Number of lists: " + lists.length); //DEBUG
  Logger.log("     "); //DEBUG

  //Loop through the Paragraphs (i)
  for (var i = 0; i < paras.length; i++) {

    var mytypeof = paras[i].getType();
    Logger.log("DEBUG: Paragraph#i=" + (i + 1) + ", Type: " + mytypeof); //DEBUG

    // if thgis parapgraph is a list item, then get more information
    if (mytypeof === DocumentApp.ElementType.LIST_ITEM) {

      var paraattributes = paras[i].getAttributes();

      // list the paragraph attributes
      for (var key in paraattributes) {
        if (paraattributes.hasOwnProperty(key)) {
          Logger.log("Paragraph Attribute: " + key + " -> " + paraattributes[key]);
        }
      }

      // List the GLPH TYPE
      Logger.log("Glyph: " + lists[listcounter].getGlyphType());
      var otherattributes = lists[listcounter].getAttributes();

      // List the List_Item Attributes
      for (var listkey in otherattributes) {
        if (otherattributes.hasOwnProperty(listkey)) {
          Logger.log("List_Item Attribute: " + listkey + " -> " + otherattributes[listkey]);
        }
      }
      listcounter = listcounter + 1;
    }
  }
}

Typical Attribute comparison - Paragraph vs List_Item
Typical Attribute comparison - Paragraph vs List_Item


List_Item Attributes
List_Item Attributes


Document layout
Document layout

Upvotes: 1

Cooper
Cooper

Reputation: 64062

I tried this:

function getGlyphType(){
  var doc=DocumentApp.getActiveDocument();
  var body=doc.getBody();
  var children=body.getNumChildren();
  var gA=[];
  for(var i=0;i<children;i++){
    var child=body.getChild(i);
    if(child.getType()==DocumentApp.ElementType.LIST_ITEM){
      gA.push(child.asListItem().getAttributes()['GLYPH_TYPE']);
    }
    var ui=HtmlService.createHtmlOutput(gA.join(', '));
    DocumentApp.getUi().showModelessDialog(ui, 'Info')
  }
}

I could get whether its a number or a bullet but I couldn't determine what type of bullet it was.

Upvotes: 0

Related Questions