使用Google Apps脚本将项目符号列表插入Google文档的首选方法是什么? 问题:解决方案:

问题描述

我正在尝试熟悉Google文档中的Google Apps脚本,并希望能够插入带有各种嵌套级别的项目符号列表。我以为我正确使用了body.insertListItem,但是奇怪的是,当我添加后续的列表项时,似乎弄乱了先前插入的项的嵌套级别和字形。

例如,这是我的示例代码

function myFunction() {
  var doc = DocumentApp.getActiveDocument()
  var body = doc.getBody()
  var cursor = doc.getCursor()
  
  var element = body.insertParagraph(0,"hello world")
  element.setheading(DocumentApp.Paragraphheading.heading1)
  var ixElement = body.getChildindex(element)
  var listItem = body.insertListItem(ixElement+1,"List")
  listItem.setnestingLevel(0)
  listItem.setGlyphType(DocumentApp.GlyphType.BULLET)
  var listItem2 = body.insertListItem(ixElement+2,"List Item")
  listItem2.setnestingLevel(1)
  listItem2.setGlyphType(DocumentApp.GlyphType.HOLLOW_BULLET)
  var listItem3 = body.insertListItem(ixElement+3,"2nd List Item")
  listItem3.setnestingLevel(1)
  listItem3.setGlyphType(DocumentApp.GlyphType.HOLLOW_BULLET)  
  var listItem4 = body.insertListItem(ixElement+4,"2x nested list Item")
  listItem4.setnestingLevel(2)
  listItem4.setGlyphType(DocumentApp.GlyphType.SQUARE_BULLET)    
  //listItem.setGlyphType(DocumentApp.GlyphType.BULLET)
}

问题在于,对“ {粘滞””的.setGlyphType的唯一调用是最后一次调用。以前的每个项目符号都会更改。

预期:

expected image

实际:

actual image

您可以在图像中看到实际结果,第一个列表项应该是项目符号,但已更改为数字。 (如果我再次致电listItem.setGlyphType(DocumentApp.GlyphType.BULLET),它将改回项目符号)。这使我认为我可能会走错路了。是否有更好的方法通过Apps脚本将listItem元素插入Google文档?

解决方法

问题:

  • body.insertListItem(index,*text*)通过具有默认字形类型的文本创建一个新列表项。这也根据嵌套级别而改变。当将此新创建的列表项附加到现有列表项时,现有列表项的字形将被覆盖。

解决方案:

  • 在插入列表项之前,创建具有所需glypetype的listItem。然后,该列表项可以与body.insertListItem(index,*listItem*)方法一起使用,而不是body.insertListItem(index,*text*)
//@OnlyCurrentDoc
function fixedMyFunction2() {
  'use strict';
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();

  var element = body.insertParagraph(0,'hello world');
  element.setHeading(DocumentApp.ParagraphHeading.HEADING1);
  var ixElement = body.getChildIndex(element);
  var listItem = body.insertListItem(ixElement + 1,'List');
  listItem.setNestingLevel(0);
  listItem.setGlyphType(DocumentApp.GlyphType.BULLET);

  var listItem2 = listItem
    .copy()
    .setNestingLevel(1)
    .setGlyphType(DocumentApp.GlyphType.HOLLOW_BULLET);
  listItem2.setText('List Item');
  body.insertListItem(ixElement + 2,listItem2);

  var listItem3 = listItem2.copy();
  listItem3.setText('2nd ListItem');
  body.insertListItem(ixElement + 3,listItem3);

  var listItem4 = listItem2
    .copy()
    .setNestingLevel(2)
    .setGlyphType(DocumentApp.GlyphType.SQUARE_BULLET);
  listItem4.setText('2x nested listItem');
  body.insertListItem(ixElement + 4,listItem4);
}
  • 无需对脚本进行大量修改。如果您在最后追加后设置了setGlyphType,这也可以工作:
//@OnlyCurrentDoc
function fixedMyFunction() {
  'use strict';
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();

  var element = body.insertParagraph(0,'List');
  listItem.setNestingLevel(0);

  var listItem2 = body.insertListItem(ixElement + 2,'List Item');
  listItem2.setNestingLevel(1);
  var listItem3 = body.insertListItem(ixElement + 3,'2nd List Item');
  listItem3.setNestingLevel(1);

  var listItem4 = body.insertListItem(ixElement + 4,'2x nested list Item');
  listItem4.setNestingLevel(2);

  //Setglyphs at last
  listItem4.setGlyphType(DocumentApp.GlyphType.SQUARE_BULLET);
  listItem3.setGlyphType(DocumentApp.GlyphType.HOLLOW_BULLET);
  listItem.setGlyphType(DocumentApp.GlyphType.BULLET);
}