【问题标题】:Get parent of a ListItem in a Google Docs file获取 Google Docs 文件中 ListItem 的父级
【发布时间】:2018-06-17 17:17:16
【问题描述】:

我正在尝试在 Google Doc 中获取 ListItem 的父 ListItemgetParent() 函数获取整个列表(例如正文)的父元素,而不是父列表项。 所以如果我有:

  • 父列表项
    • 列表项

对于“列表项”元素,我希望返回的父项是“父列表项”元素。 “父列表项”的父项将为空。

关于如何做到这一点的任何想法?

【问题讨论】:

    标签: google-apps-script google-docs


    【解决方案1】:

    正如您所注意到的,没有提供任何方法可以满足您的需求。因此,您只需使用提供的功能将一些东西放在一起。

    这是一个快速尝试实现一个给定列表项的函数,如果找到则返回父列表项。否则,如果未找到父列表项(即仅包含多个缩进项的列表)或它已经处于顶级缩进级别,则返回 null

    function getParentListItem(listItem) {
      if (listItem.getType() != DocumentApp.ElementType.LIST_ITEM) {
        return null;
      }
      var currentNestingLevel = listItem.getNestingLevel();
      if (currentNestingLevel == 0) {
        return null;
      }
      var sibling = listItem.getPreviousSibling();
      while (sibling) {
        var siblingNestedLevel = sibling.getNestingLevel();
        if (siblingNestedLevel < currentNestingLevel) {
          return sibling;
        }
        sibling = sibling.getPreviousSibling();
      }
      return null;
    }
    

    我使用包含此内容的文档进行了测试:

    Not a list.
    - Item 1
    - Item 2
       - Nested Item 1
       - Nested Item 2
          - Double Nested Item 1
    - Item 3
    Not a list.
    - List 2 Item 1
       - List 2 Nested Item 1
    

    并使用此功能:

    function test_getParentListItem() {
      var body = DocumentApp.getActiveDocument().getBody();
      for (var i = 0; i < body.getNumChildren(); i++) { 
        var child = body.getChild(i);
        if (child.getType() != DocumentApp.ElementType.LIST_ITEM) {
          Logger.log("Not a list item.");
          continue;
        }
        Logger.log("Looking up parent list item for: " + child.getText());
        var parentItem = getParentListItem(child);
        if (parentItem != null) {
          Logger.log("Parent list item: " + parentItem.getText());
        } else {
          Logger.log("No parent item found."); 
        }
      }
    }
    

    这导致了这个日志输出:

    [18-06-17 16:59:15:513 PDT] Not a list item
    [18-06-17 16:59:15:515 PDT] Looking up parent list item for: Item 1
    [18-06-17 16:59:15:516 PDT] No parent item found.
    [18-06-17 16:59:15:518 PDT] Looking up parent list item for: Item 2
    [18-06-17 16:59:15:519 PDT] No parent item found.
    [18-06-17 16:59:15:524 PDT] Looking up parent list item for: Nested Item 1
    [18-06-17 16:59:15:527 PDT] Parent list item: Item 2
    [18-06-17 16:59:15:528 PDT] Looking up parent list item for: Nested Item 2
    [18-06-17 16:59:15:531 PDT] Parent list item: Item 2
    [18-06-17 16:59:15:533 PDT] Looking up parent list item for: Double Nested Item 1
    [18-06-17 16:59:15:536 PDT] Parent list item: Nested Item 2
    [18-06-17 16:59:15:538 PDT] Looking up parent list item for: Item 3
    [18-06-17 16:59:15:539 PDT] No parent item found.
    [18-06-17 16:59:15:541 PDT] Not a list item
    [18-06-17 16:59:15:543 PDT] Looking up parent list item for: List 2 Item 1
    [18-06-17 16:59:15:544 PDT] No parent item found.
    [18-06-17 16:59:15:546 PDT] Looking up parent list item for: List 2 Nested Item 1
    [18-06-17 16:59:15:549 PDT] Parent list item: List 2 Item 1
    

    【讨论】:

    • 今天下午出去的时候我也想到了同样的问题,当我看到你建议的编辑时,我正在做一个类似的编辑。稍微修改了您的编辑,在函数本身中添加了类似的检查,并更新了示例以更加彻底。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 2018-06-25
    • 2021-06-20
    相关资源
    最近更新 更多