DocumentApp Paragraph returning incorrect attributes

125 views
Skip to first unread message

justin ferguson

unread,
Oct 15, 2021, 12:17:20 PM10/15/21
to Google Apps Script Community
I'm working in Docs with AppsScript, and it seems to me that "getAttributes" on a paragraph is not returning expected values.   

This gist (https://gist.github.com/fiveable-jferg/3db38ac3a2f2beb228cff1d6e41aff43) has two JSON fragments of the same portion of the same file.  The first one is from the object generated by Docs.Documents.get(DocumentApp.getActiveDocument().getId()), and the second is generated by custom code that uses DocumentApp.Element.getAttributes() to pull the attributes for each node.   The issue I'm having is that getAttributes() is not returning a value for the BACKGROUND_COLOR attribute (as seen on line 166 of the second file in the gist), but there _is_ a shading.backgroundColor set for the paragraph (see line 72 of the first file in the gist).

Any suggestions how I can get the background color for the paragraph using the DocumentApp API?  

JF

Clark Lind

unread,
Oct 16, 2021, 2:06:02 PM10/16/21
to Google Apps Script Community
You can mess around and see if something like this gets you anywhere (untested):
const bgColor = DocumentApp.Element.getAttributes( DocumentApp.Attribute.BACKGROUND_COLOR ) ;
console.log(bgColor)

justin ferguson

unread,
Oct 16, 2021, 3:07:44 PM10/16/21
to Google Apps Script Community
Thanks.  The expected argument for getAttributes is either none, or a location within the element, not the name of the Attribute to get, so that doesn't help.  

I've tried the following: 

function myFunction() {
  let document = DocumentApp.getActiveDocument();
  let node = DocumentApp.getActiveDocument().getBody().getChild(4);
  Logger.log(node.getType());
  Logger.log(node.asParagraph().getText());
  Logger.log(JSON.stringify(node.asText().getAttributes()));
  Logger.log(node.asText().getBackgroundColor());
}

which results in:  

[21-10-16 13:41:49:141 CDT] PARAGRAPH
[21-10-16 13:41:49:144 CDT] 📄 Study AP European History, Unit 3: Absolutism and Constitutionalism
[21-10-16 13:41:49:149 CDT] {"STRIKETHROUGH":null,"LINK_URL":null,"SPACING_BEFORE":null,"BACKGROUND_COLOR":null,"LINE_SPACING":null,"BOLD":null,"HEADING":"NORMAL","INDENT_START":null,"HORIZONTAL_ALIGNMENT":null,"INDENT_END":null,"LEFT_TO_RIGHT":true,"UNDERLINE":null,"FONT_FAMILY":"Work Sans","SPACING_AFTER":null,"ITALIC":null,"FOREGROUND_COLOR":null,"FONT_SIZE":null,"INDENT_FIRST_LINE":null}
[21-10-16 13:41:49:153 CDT] null

None of which lets me get the background color for the paragraph.

JF

Clark Lind

unread,
Oct 21, 2021, 7:48:50 PM10/21/21
to Google Apps Script Community
Sorry it has taken so long to reply. I did get this working using advanced Document service:

Doc example:
bgcolor.PNG
Function:
function getBackgroundColors() {
  var documentId = DocumentApp.getActiveDocument().getId()
  var doc = Docs.Documents.get(documentId)
  var content = doc.body.content

   content.forEach( (el) => {
     if (el.paragraph) {
       el.paragraph.elements.forEach( (element) => {
         console.log(element.textRun.textStyle.backgroundColor)
       })
     }
   })
}

Result:
7:38:05 PM  Info  undefined
7:38:05 PM  Info  { color: { rgbColor: { green: 1, blue: 1 } } }
7:38:05 PM  Info  { color: { rgbColor: { red: 0.9019608, green: 0.5686275, blue: 0.21960784 } } }

So, to get to the bgcolor, it is buried deep:  
paragraph .elements .textRun .textStyle .backgroundColor .color .rgbColor[red, green, blue]  

It looks like the rgb values are percentages (0-100)


Reply all
Reply to author
Forward
0 new messages