Google Apps 脚本的 DocumentApp 未返回元素的所有属性?
DocumentApp for Google Apps Script not returning all attributes of an element?
当我尝试使用 Google Apps Script DocumentApp 库中 Google Doc 的 getAttributes() 方法从元素获取属性时,并非所有属性都会显示;特别是 listItem 元素的 GLYPH_TYPE 属性。
由于 Google 清单文档中缺少功能,我正在编写一个代码脚本,根据属于列表的每个列表项的符号自动计算列表的完成百分比.为此,我正在检索与 listID 相关的所有 listItems,然后尝试检查它们的 GLYPH_TYPE 属性以区分哪些项目已完成(用户通过单击列表左侧的符号指示)项目并将其更改为复选标记)。我遇到的是后者;当我为列表项的段落或 listItem 对象调用 getAttributes() 时,GLYPH_TYPE 根本不存在。
这里有一个方法,我只是尝试提取 listItem 的 GLYPH_TYPE 属性;
function getGlyphTypeFromListItem(documentId){
var doc =
DocumentApp.openById("1mzVQUccSH_suf8KoTkbVN4XjIOcfbYDuie3GV_M1Fg8");
var body = doc.getBody();
Logger.log(body.getParagraphs()[0].getAttributes());
Logger.log(body.getParagraphs()
[0].findElement(DocumentApp.ElementType.LIST_ITEM).getElement()
.getAttributes());
}
当我 运行 使用此方法时,我得到了响应:
[19-01-20 18:22:02:242 MST] {FONT_SIZE=11, ITALIC=true, HORIZONTAL_ALIGNMENT=null, INDENT_END=null, INDENT_START=144.0,LINE_SPACING=null, LINK_URL=null, UNDERLINE=true,
BACKGROUND_COLOR=null, INDENT_FIRST_LINE=126.0, LEFT_TO_RIGHT=true,
SPACING_BEFORE=null, HEADING=Normal, SPACING_AFTER=null,
STRIKETHROUGH=null, FOREGROUND_COLOR=null, BOLD=true,
FONT_FAMILY=Roboto Condensed}
[19-01-20 18:30:21:253 MST] {FONT_SIZE=11, ITALIC=true,
HORIZONTAL_ALIGNMENT=null, INDENT_END=null, INDENT_START=144.0,
LINE_SPACING=null, LINK_URL=null, UNDERLINE=true, BACKGROUND_COLOR=null,
INDENT_FIRST_LINE=126.0, LEFT_TO_RIGHT=true, SPACING_BEFORE=null,
HEADING=Normal, SPACING_AFTER=null, STRIKETHROUGH=null, FOREGROUND_COLOR=null,
BOLD=true, FONT_FAMILY=Roboto Condensed}
如您所见,GLYPH_TYPE 完全不存在于两个日志中;有什么我想念的吗?另外,你们有没有找到一种更直观的方法来跟踪 Google Docs 中 "checklists" 的完成情况?
提前致谢!
我试过这个:
function getGlyphType(){
var doc=DocumentApp.getActiveDocument();
var body=doc.getBody();
var children=body.getNumChildren();
var gA=[];
for(var i=0;i<children;i++){
var child=body.getChild(i);
if(child.getType()==DocumentApp.ElementType.LIST_ITEM){
gA.push(child.asListItem().getAttributes()['GLYPH_TYPE']);
}
var ui=HtmlService.createHtmlOutput(gA.join(', '));
DocumentApp.getUi().showModelessDialog(ui, 'Info')
}
}
我可以知道它是一个数字还是一颗子弹,但我无法确定它是什么类型的子弹。
Google 文档说 "Paragraph" getAtributes
方法 (doc) "retrieves the element's attributes. The result is an object containing a property for each valid element attribute where each property name corresponds to an item in the DocumentApp.Attribute enumeration." GLYPH_TYPE
is recorded as one of the properties of "Enum Attribute”。
我同意 OP 的观点,即在分析段落时不报告 GLYPH TYPE
。
但是,如果段落类型 = DocumentApp.ElementType.LIST_ITEM
,则 getAttributes
确实 报告 GLYPH_TYPE
,以及一些额外的数据(参考下图)。很难说 Google 代码中是否存在错误,或者(也许)文档中存在错误。
就OP代码而言,我得到了相同的结果。
但有趣的是,getParagraphs
返回了 ListItem,但 ListItem 不是 'paragraph'。 ("A Paragraph may contain Equation, Footnote, HorizontalRule, InlineDrawing, InlineImage, PageBreak, and Text elements." (docs)). The getParagraphs
method "retrieves all the Paragraphs contained in the section (including ListItems)" (docs)。这是一个奇怪的,几乎是矛盾的结果,但我认为这可能是 OP 的代码不成功的原因。
这是我的代码(改编自 OP)。
function so54282539() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
// variable to count the list#
var listcounter = 0;
// get the Paragraphs
var paras = doc.getParagraphs();
//Logger.log("DEBUG: paras = "+paras);//DEBUG
Logger.log("DEBUG:Number of paragraphs: " + paras.length); //DEBUG
// get the List Items
var lists = doc.getListItems();
//Logger.log("DEBUG: Lists: "+lists);//DEBUG
Logger.log("DEBUG: Number of lists: " + lists.length); //DEBUG
Logger.log(" "); //DEBUG
//Loop through the Paragraphs (i)
for (var i = 0; i < paras.length; i++) {
var mytypeof = paras[i].getType();
Logger.log("DEBUG: Paragraph#i=" + (i + 1) + ", Type: " + mytypeof); //DEBUG
// if thgis parapgraph is a list item, then get more information
if (mytypeof === DocumentApp.ElementType.LIST_ITEM) {
var paraattributes = paras[i].getAttributes();
// list the paragraph attributes
for (var key in paraattributes) {
if (paraattributes.hasOwnProperty(key)) {
Logger.log("Paragraph Attribute: " + key + " -> " + paraattributes[key]);
}
}
// List the GLPH TYPE
Logger.log("Glyph: " + lists[listcounter].getGlyphType());
var otherattributes = lists[listcounter].getAttributes();
// List the List_Item Attributes
for (var listkey in otherattributes) {
if (otherattributes.hasOwnProperty(listkey)) {
Logger.log("List_Item Attribute: " + listkey + " -> " + otherattributes[listkey]);
}
}
listcounter = listcounter + 1;
}
}
}
典型属性比较 - 段落 vs List_Item
List_Item 属性
文档布局
当我尝试使用 Google Apps Script DocumentApp 库中 Google Doc 的 getAttributes() 方法从元素获取属性时,并非所有属性都会显示;特别是 listItem 元素的 GLYPH_TYPE 属性。
由于 Google 清单文档中缺少功能,我正在编写一个代码脚本,根据属于列表的每个列表项的符号自动计算列表的完成百分比.为此,我正在检索与 listID 相关的所有 listItems,然后尝试检查它们的 GLYPH_TYPE 属性以区分哪些项目已完成(用户通过单击列表左侧的符号指示)项目并将其更改为复选标记)。我遇到的是后者;当我为列表项的段落或 listItem 对象调用 getAttributes() 时,GLYPH_TYPE 根本不存在。
这里有一个方法,我只是尝试提取 listItem 的 GLYPH_TYPE 属性;
function getGlyphTypeFromListItem(documentId){
var doc =
DocumentApp.openById("1mzVQUccSH_suf8KoTkbVN4XjIOcfbYDuie3GV_M1Fg8");
var body = doc.getBody();
Logger.log(body.getParagraphs()[0].getAttributes());
Logger.log(body.getParagraphs()
[0].findElement(DocumentApp.ElementType.LIST_ITEM).getElement()
.getAttributes());
}
当我 运行 使用此方法时,我得到了响应:
[19-01-20 18:22:02:242 MST] {FONT_SIZE=11, ITALIC=true, HORIZONTAL_ALIGNMENT=null, INDENT_END=null, INDENT_START=144.0,LINE_SPACING=null, LINK_URL=null, UNDERLINE=true,
BACKGROUND_COLOR=null, INDENT_FIRST_LINE=126.0, LEFT_TO_RIGHT=true,
SPACING_BEFORE=null, HEADING=Normal, SPACING_AFTER=null,
STRIKETHROUGH=null, FOREGROUND_COLOR=null, BOLD=true,
FONT_FAMILY=Roboto Condensed}
[19-01-20 18:30:21:253 MST] {FONT_SIZE=11, ITALIC=true,
HORIZONTAL_ALIGNMENT=null, INDENT_END=null, INDENT_START=144.0,
LINE_SPACING=null, LINK_URL=null, UNDERLINE=true, BACKGROUND_COLOR=null,
INDENT_FIRST_LINE=126.0, LEFT_TO_RIGHT=true, SPACING_BEFORE=null,
HEADING=Normal, SPACING_AFTER=null, STRIKETHROUGH=null, FOREGROUND_COLOR=null,
BOLD=true, FONT_FAMILY=Roboto Condensed}
如您所见,GLYPH_TYPE 完全不存在于两个日志中;有什么我想念的吗?另外,你们有没有找到一种更直观的方法来跟踪 Google Docs 中 "checklists" 的完成情况?
提前致谢!
我试过这个:
function getGlyphType(){
var doc=DocumentApp.getActiveDocument();
var body=doc.getBody();
var children=body.getNumChildren();
var gA=[];
for(var i=0;i<children;i++){
var child=body.getChild(i);
if(child.getType()==DocumentApp.ElementType.LIST_ITEM){
gA.push(child.asListItem().getAttributes()['GLYPH_TYPE']);
}
var ui=HtmlService.createHtmlOutput(gA.join(', '));
DocumentApp.getUi().showModelessDialog(ui, 'Info')
}
}
我可以知道它是一个数字还是一颗子弹,但我无法确定它是什么类型的子弹。
Google 文档说 "Paragraph" getAtributes
方法 (doc) "retrieves the element's attributes. The result is an object containing a property for each valid element attribute where each property name corresponds to an item in the DocumentApp.Attribute enumeration." GLYPH_TYPE
is recorded as one of the properties of "Enum Attribute”。
我同意 OP 的观点,即在分析段落时不报告 GLYPH TYPE
。
但是,如果段落类型 = DocumentApp.ElementType.LIST_ITEM
,则 getAttributes
确实 报告 GLYPH_TYPE
,以及一些额外的数据(参考下图)。很难说 Google 代码中是否存在错误,或者(也许)文档中存在错误。
就OP代码而言,我得到了相同的结果。
但有趣的是,getParagraphs
返回了 ListItem,但 ListItem 不是 'paragraph'。 ("A Paragraph may contain Equation, Footnote, HorizontalRule, InlineDrawing, InlineImage, PageBreak, and Text elements." (docs)). The getParagraphs
method "retrieves all the Paragraphs contained in the section (including ListItems)" (docs)。这是一个奇怪的,几乎是矛盾的结果,但我认为这可能是 OP 的代码不成功的原因。
这是我的代码(改编自 OP)。
function so54282539() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
// variable to count the list#
var listcounter = 0;
// get the Paragraphs
var paras = doc.getParagraphs();
//Logger.log("DEBUG: paras = "+paras);//DEBUG
Logger.log("DEBUG:Number of paragraphs: " + paras.length); //DEBUG
// get the List Items
var lists = doc.getListItems();
//Logger.log("DEBUG: Lists: "+lists);//DEBUG
Logger.log("DEBUG: Number of lists: " + lists.length); //DEBUG
Logger.log(" "); //DEBUG
//Loop through the Paragraphs (i)
for (var i = 0; i < paras.length; i++) {
var mytypeof = paras[i].getType();
Logger.log("DEBUG: Paragraph#i=" + (i + 1) + ", Type: " + mytypeof); //DEBUG
// if thgis parapgraph is a list item, then get more information
if (mytypeof === DocumentApp.ElementType.LIST_ITEM) {
var paraattributes = paras[i].getAttributes();
// list the paragraph attributes
for (var key in paraattributes) {
if (paraattributes.hasOwnProperty(key)) {
Logger.log("Paragraph Attribute: " + key + " -> " + paraattributes[key]);
}
}
// List the GLPH TYPE
Logger.log("Glyph: " + lists[listcounter].getGlyphType());
var otherattributes = lists[listcounter].getAttributes();
// List the List_Item Attributes
for (var listkey in otherattributes) {
if (otherattributes.hasOwnProperty(listkey)) {
Logger.log("List_Item Attribute: " + listkey + " -> " + otherattributes[listkey]);
}
}
listcounter = listcounter + 1;
}
}
}
典型属性比较 - 段落 vs List_Item
List_Item 属性
文档布局