2
votes

I am trying to read Attributes of a Paragraph from a Google Document using Apps Script. When I read the attributes, some BOLD attributes are returned as null.

Here is my sample script to read the attributes.

// Reads the attributes of every paragraph in this Google Document and print all paragraph text that is BOLD

function readAttributes() {
  var doc = DocumentApp.getActiveDocument(); 
  var body = doc.getBody(); 
  var paras = body.getParagraphs();
  for (var i = 0; i < paras.length; i++){ 
    var paragraph = paras[i];  
    var attribute = paragraph.getAttributes(); 
    if (attribute.BOLD === true) {
      Logger.log(paragraph.getText()); 
    }
  } 
}

Here is a simulated sample document: The script is behind this Google document. File -> Make a copy.

https://docs.google.com/document/d/13FYg8AAk6PX9TEUdgfaT-60Vi5xoQlZ9Moink5guLH0/edit?usp=sharing

What is wrong with my document? Only the attribute of Question 18 is returned as BOLD and that of Question 17 is returned as null.

The returned Attribute object for any paragraph in Google Document looks like below.

 {
    FONT_SIZE=null, 
    ITALIC=null, 
    HORIZONTAL_ALIGNMENT=null, 
    INDENT_END=null,
    INDENT_START=null, 
    LINE_SPACING=1.0, 
    LINK_URL=null, 
    UNDERLINE=null, 
    BACKGROUND_COLOR=null, 
    INDENT_FIRST_LINE=null, 
    LEFT_TO_RIGHT=true, 
    SPACING_BEFORE=null, 
    HEADING=Normal, 
    SPACING_AFTER=null, 
    STRIKETHROUGH=null, 
    FOREGROUND_COLOR=null, 
    BOLD=null, 
    FONT_FAMILY=Calibri
}

My Logger.log() Results:

Expected output :

  1. Which of the following is the most effective plan of action to take if your vehicle catches fire while you are driving?
  2. Before taking a trip, you need to inspect your tires. Which problems would require immediate action?

Resulted output:

  1. Before taking a trip, you need to inspect your tires. Which problems would require immediate action?

Image showing the problem

So clearly Question 17 in the document has a problem and so its BOLD attribute is null.

1
Not sure, but the space between effective and plan is both italic and bold. Maybe the the paragraph as a whole isn't considered bold, but just the individual text elements. Maybe try paragraph.editAsText().getAttribute(offset) ? - TheMaster
@I'-'I For example, there is a paragraph with text of sample that all characters except for a are BOLD attribute. When paragraph.editAsText().getAttributes(offset) is used to search BOLD attribute, when only one normal character (a) is included in there, it seems that the property of BOLD becomes null for all offset. This might be the specification. But when isBold(offset) is used, even if one normal character is included in the paragraph, the characters except for a show true. - Tanaike

1 Answers

1
votes

In your sample, in the paragraph of 17. Which of ..., whole paragraph is not BOLD attribute. On the other hand, in the paragraph of 18. Before taking..., whole paragraph is BOLD attribute. By this, the BOLD attribute of the paragraph of 17. Which of ... becomes null. So how about this modification? I think that there are several answers for your situation. So please think of this as one of them.

Modification points:

  • About each paragraph, scan the text every character. And retrieve the character with the BOLD attribute.
    • For this situation, it uses isBold().

Modified script:

Please modify as follows.

for (var i = 0; i < paras.length; i++){ 
  var paragraph = paras[i];  
  var attribute = paragraph.getAttributes(); 
  if (attribute.BOLD === true) {
    Logger.log(paragraph.getText()); 
  }
} 
for (var i = 0; i < paras.length; i++) {
  var res = "";
  var paragraph = paras[i];
  var attribute = paragraph.editAsText();
  for (var j = 0; j < attribute.getText().length; j++) {
    if (attribute.isBold(j)) {
      res += attribute.getText()[j];
    }
  }
  if (res.length > 0) {
    Logger.log(res)
  }
}

Result:

  1. Which of the following is the most effective plan of action to take if your vehicle catches fire while you are driving?
  2. Before taking a trip, you need to inspect your tires. Which problems would require immediate action?

Note:

  • In this modification, if the characters which are not the BOLD attribute are included in a sentence, such characters are not retrieved. If all characters are the BOLD attribute, whole sentence is retrieved. Please be careful this.

Reference:

If this was not what you want, I'm sorry.