JohnRDOrazio
JohnRDOrazio

Reputation: 1534

How to retrieve font-family from document with Google Apps Script

I'm not having much luck in getting the font-family attribute from selected text in a document using Google Apps Script.

  var selection = DocumentApp.getActiveDocument().getSelection();
  if (!selection) {
    DocumentApp.getUi().alert('Cannot find a selection in the document.');
    return;
  }
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();    
  var selectedElements = selection.getSelectedElements();
  var element = selectedElements[0].getElement(); //maybe add .asText() ?
  var attrs = element.getAttributes(0); // I notice that by giving a specific offset I get the color attribute
  for (var att in attrs) {
    Logger.log(att + " : " + attrs[att]);
  }
  //Not much luck, font-family is null
  //let's try getting just font-family explicitly
  var fontfam = element.getFontFamily(0);
  Logger.log("font-family : " + fontfam);
  //again, font-family is null!

What is it that I am missing, how can I retrieve the font-family at offset 0 of the currently selected text?

UPDATE: After some trial and error, I see that this happens for any text formatted with the "Arial" font, while for any other font there is no problem, so I've opened an issue for it https://code.google.com/p/google-apps-script-issues/issues/detail?id=4286 .

UPDATE: Seems to happen for other fonts also, such as "EB Garamond", "Gloria Halleluia", "Great Vibes", "Pacifico", "Poiret One", "Shadows into Light" (installed as extra fonts), whereas it doesn't happen for other fonts installed from Google Fonts ("Droid Serif", "Drois Sans", "Ubuntu", "Merriweather", "Dancing script" all work fine for example).

Upvotes: 0

Views: 1687

Answers (1)

Serge insas
Serge insas

Reputation: 46802

Using your exact code I get this result : (null is reported for default values in the doc)

enter image description here

and the doc was as follows :

enter image description here

Upvotes: 1

Related Questions