【问题标题】:insert items in a list在列表中插入项目
【发布时间】:2018-05-18 01:45:58
【问题描述】:

我在我的文档中搜索一个占位符,它是单个列表项(例如,body.findText('{{list}}')。我有一组要插入到此列表中的项。

一旦我有了一个 listItem 元素,如何向它附加更多 listItems?我希望会有类似foundListItem.getParent().appendListItem(items[i])

我已经搜索了这个问题的答案,并且看到了一些需要付出巨大努力才能完成的答案。 必须有一个更简单的方法!例如将它们附加到底部并弄清楚将其移动到哪里是一个荒谬的答案。

【问题讨论】:

  • 您可以查看this SO question 中发布的答案,因为它们展示了如何获取文档的特定部分并设置其内容。
  • @Casper 我已经看到了许多替换文本的解决方案,这是有道理的;但是,我正在努力添加 ListItems。看来我只能在正文级别执行此操作,并且在创建它们之后将它们移动到原始 listID 或其他内容。这似乎是很多代码,并且在父/子树中跳转来做一些简单的事情,比如修改列表中的项目。

标签: google-apps-script google-docs


【解决方案1】:

看起来很麻烦,Lists 是由 ListItems 组成的,但似乎没有 List 对象。有一个 ListId,但它似乎没有任何实际功能,除了您可以拆分列表并继续数字。您似乎只能将 ListItem 添加到正文中,但似乎没有一种简单的方法可以获取 ListItem 的索引以便您可以在它之后追加。

我编写了一个函数,它将遍历正文中的所有项目以查找带有一些占位符文本的 ListItem 并返回其索引。

function findListItemWithText(text) {

  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var index = -1;

  for (var i=0; i<body.getNumChildren(); i++) {
    var child = body.getChild(i);

    if (child.getType() ==  DocumentApp.ElementType.LIST_ITEM) {

      var listItem = child.asListItem();
      if (listItem.getText() == text) {
         index = i;
      }
    }
  }
  return index;
}

然后我编写了一个函数,将 ListItem 替换为数组中的元素:

function replaceListItem (placeholder, list) {

  var index = findListItemWithText(placeholder);
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();

  var listItem = body.getChild(index).asListItem();

  // replace the text in the placeholder ListItem
  listItem.setText(list[0]);

  // append the rest of the list after the placeholder ListItem
  for (var i=1; i<list.length; i++) {
    body.insertListItem(index + i, list[i]);  
  }
}

然后您可以使用占位符和列表调用此函数,如果存在带有此文本的 ListItem,它将在该点附加 ListItems 列表。

replaceListItem("{{list}}", ["One", "Two", "Three"]);

它适用于编号列表和项目符号列表。如果有两个或更多占位符,它将只替换最后一个。

如果有人可以对此提出批评,我很想知道找到和操作给定元素的更有效方法,因为我的解决方案似乎比我预期的要多得多。

【讨论】:

    【解决方案2】:

    我能够使用此代码实现所需的行为:

    function updateList(){
      var listItem = DocumentApp.getActiveDocument().getBody().getListItems()[0]
      var list = listItem.getParent()
      var index = list.getChildIndex(listItem)
      debug && Logger.log('ListItem ['+index+'] parent: '+list.getType()) // debug=true logging
    
      // remove listItem
      list.removeChild(listItem)
    
      // add new children
      var names = getList() // function retrieves a list of rows from spreadsheet.
      names.forEach(function (v){
        list.insertListItem(index++, v[1]) // the second element is the name
      })
    }
    

    请注意,这不是使用模板字符串执行此操作,这一次对我来说没问题;但是,要找到模板字符串 '{{list}}' 并将其替换为我发现可行的数据:

    function findListItem(str) {
      if (str == undefined) str = '{{list}}'
      debug && Logger.log(str)
      var a = ['one', 'two', 'three']
      var body = DocumentApp.getActiveDocument().getBody()
      var match = body.findText(str)
      if (match == null) {
        debug && Logger.log('No match')
        return
      }
      var listItem = match.getElement().getParent()
      var list = listItem.getParent()
      var idx = list.getChildIndex(listItem)
      listItem.removeFromParent()
      a.forEach(function(s) {
        list.insertListItem(idx++, s)
      })
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      相关资源
      最近更新 更多