【问题标题】:Change the paragraph type to list item type using GAS使用 GAS 将段落类型更改为列表项类型
【发布时间】:2020-08-14 02:25:42
【问题描述】:

我想更改段落以列出以## 开头的段落类型。

docs文档是here

我试过了

function doGet(e) {
    var doc = DocumentApp.openByUrl('https://docs.google.com/document/d/1TRo9RG6R2ZOBqXQEJtBhRBLKzu9XOlFycamYplkma14/edit');
    var body = doc.getBody();
    var paras = body.getParagraphs();
    for (var i = 0; i < paras.length; i++) {
        if (paras[i].editAsText().getText().indexOf("##") == 0) {
            //var listItem = body.insertListItem(childIndex, listItem)
            var text = paras[i].editAsText().getText()
            var childIndex = body.getChildIndex(paras[i])
            var element = body.removeChild(paras[i])
            element.appendListItem(text).setGlyphType(DocumentApp.GlyphType.BULLET)
        }
    }
    return ContentService.createTextOutput("Success" + body.getChildIndex(paras[3]));
}

【问题讨论】:

    标签: google-apps-script google-docs google-docs-api


    【解决方案1】:

    你快到了。但我会看Body.insertListItem() 方法而不是Body.appendListItem() 方法。

    例如,您可以将 for 循环更改为以下内容:

    for (var i = 0; i < paras.length; i++) {
      if (paras[i].editAsText().getText().indexOf("##") == 0) {
        // This element is a future list item
        var elem = paras[i];
        // What's the text going to be?
        var text = elem.editAsText().getText()
        // Where will it be inserted?
        var childIndex = body.getChildIndex(elem)
        // I should remove the old element
        body.removeChild(elem)
        // And take it's place with this new one with Glyph.
        body.insertListItem(childIndex, text).setGlyphType(DocumentApp.GlyphType.BULLET)
      }
    }
    

    参考资料:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-06
      • 2021-07-19
      • 1970-01-01
      • 2019-11-08
      • 1970-01-01
      相关资源
      最近更新 更多