Virender Thakur
Virender Thakur

Reputation: 519

Google App Script: Getting NULL values from getAttributes

I want to get all the attributes like(BOLD,UNDERLINE,FOREGROUND_COLOR,FONT_SIZE etc) from google doc. But I am getting NULL values while using getAttributes. Following is my code.

var fields = doc.getNamedRanges("myFields");
var lastFontColor; 
for(var i = 0; i < fields.length; i++)
{
  var rangeElement = fields[i].getRange().getRangeElements()[0];      
  var obj= rangeElement.getElement().asText().editAsText();
  var element= rangeElement.getElement().asText();  
  var atts = obj.getAttributes(); 
  for (var att in atts) 
  {
    Logger.log("value of " + att + ":" + atts[att]);
  }
}

the ouput I am getting:-

FONT_SIZE: null
ITALIC: null
STRIKETHROUGH: null
FOREGROUND_COLOR: #0000ff \ fore colors comes for some of elements.
BOLD: null

Please let me know if there is any other method to do this.

Upvotes: 1

Views: 810

Answers (1)

Anton Dementiev
Anton Dementiev

Reputation: 5716

Unfortunately, this has been a recurring issue in Google Docs. Some of the style attributes return 'null' unless set explicitly in code.

For example, after executing the following document-bound script, I get 'null' values for FONT_SIZE, FONT_FAMILY, and some other attributes while PAGE_MARGIN and PAGE_WIDTH display correct values:

 var doc = DocumentApp.getActiveDocument();
 var body = doc.getBody();     
 var styles = body.getAttributes();      
 Logger.log(styles);

However, if you explicitly set these attributes in your script, logging the output of body.getAttributes() will return these new values

  var styles = {};
  styles[DocumentApp.Attribute.FONT_FAMILY] = "Times New Roman";
  styles[DocumentApp.Attribute.FONT_SIZE] = 16;
  body.setAttributes(styles);

Upvotes: 1

Related Questions