【问题标题】:How to retrieve font-family from document with Google Apps Script如何使用 Google Apps 脚本从文档中检索字体系列
【发布时间】:2017-10-27 12:20:35
【问题描述】:

我在使用 Google Apps 脚本从文档中的选定文本中获取 font-family 属性时运气不佳。

  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!

我缺少什么,如何在当前选定文本的偏移量 0 处检索字体系列?

更新:经过反复试验,我发现任何使用“Arial”字体格式化的文本都会出现这种情况,而对于任何其他字体都没有问题,所以我打开了一个给它发https://code.google.com/p/google-apps-script-issues/issues/detail?id=4286 .

更新:似乎也适用于其他字体,例如“EB Garamond”、“Gloria Halleluia”、“Great Vibes”、“Pacifico”、“Poiret One”、“Shadows into Light” "(作为额外字体安装),而从 Google Fonts 安装的其他字体不会发生这种情况(例如,“Droid Serif”、“Drois Sans”、“Ubuntu”、“Merriweather”、“Dancing script”都可以正常工作) .

【问题讨论】:

    标签: google-apps-script google-docs


    【解决方案1】:

    使用您的确切代码,我得到以下结果:(文档中的默认值报告为 null)

    文档如下:

    【讨论】:

    • 即使我改变了字体系列(所以它不再是默认的)我一直在变空......我想知道有什么不同? [14-08-19 15:37:51:672 CEST] UNDERLINE : null [14-08-19 15:37:51:672 CEST] LINK_URL : null [14-08-19 15:37:51:672 CEST] ITALIC : false [14-08-19 15:37:51:673 CEST] BOLD : true [14-08-19 15:37:51:673 CEST] BACKGROUND_COLOR : null [14-08-19 15:37:51:673 CEST] FONT_SIZE : 10 [14-08-19 15:37:51:673 CEST] FONT_FAMILY : null [14-08-19 15:37:51:674 CEST] STRIKETHROUGH : null [14-08-19 15:37:51:674 CEST] FOREGROUND_COLOR : #000044 [14-08-19 15:37:51:674 CEST] font-family : null
    • 我注意到,如果我将字体更改为“Consolas”,结果会得到“Consolas”,如果将其更改为“Arial”,结果会得到 null。经过多次尝试,我发现只有“Arial”给出了 null,即使我将文本设置为 Arial(所以它不仅仅是默认值)。
    猜你喜欢
    • 2019-02-13
    • 2016-06-26
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多