使用Google Apps脚本将项目符号列表插入Google文档的首选方法是什么? [英] What's the preferred way to insert a bulleted list into a Google Doc using Google Apps Script?

查看:49
本文介绍了使用Google Apps脚本将项目符号列表插入Google文档的首选方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get familiar with Google Apps Script in a Google doc and want to be able to insert a bulleted list with various levels of nesting. I thought I was using body.insertListItem correctly, but what's odd is that when I add subsequent list item, it seems to mess up the nesting levels and glyphs of previously inserted items.

例如,这是我的示例代码:

For example, here's my sample code:

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 的唯一调用会粘贴"是最后一个被调用的.以前的每个项目符号都会更改.

The problem is that is that the only call to .setGlyphType that "sticks" is the last one that's called. Each previous bullet gets changed.

预期:

实际:

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

You can see in the image for the actual results that the first list item should have been a bullet, but was changed to a number. (If I call listItem.setGlyphType(DocumentApp.GlyphType.BULLET) again it will change back to a bullet). This makes me think I may be going about this the wrong way. Is there a better way to insert listItem elements into a Google Doc with Apps Script?

推荐答案

问题:

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

    • body.insertListItem(index, *text*) creates a new list item from text with a default glype type. This also changes according to the nesting level. When this newly created list item is appended to the existing list item, the existing list item's glype is overwritten.
      • 在插入列表项之前,创建具有所需glypetype的listItem.然后,此列表项可以与 body.insertListItem(index,* listItem *)方法一起使用,而不是 body.insertListItem(index,* text *)
      • Create a listItem with the required glypetype before inserting the list item. This list item can then be used with body.insertListItem(index, *listItem*) method instead of 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 s,这也将起作用:
        • Without much modifications to your script. This also works, if you set setGlyphTypes after appending at last :
        • //@OnlyCurrentDoc
          function fixedMyFunction() {
            '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);
          
            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);
          }
          

          这篇关于使用Google Apps脚本将项目符号列表插入Google文档的首选方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆