【发布时间】: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