【问题标题】:How to insert a paragraph object in listItem preserving the formating of each word of the paragraph?如何在列表项中插入段落对象,保留段落每个单词的格式?
【发布时间】:2013-12-22 16:27:40
【问题描述】:

在documentation sample 之后,我正在尝试创建一个函数来搜索谷歌文档中的数字列表,如果找到它,则将新项目添加到列表中。我的代码适用于字符串(感谢@Serge insas 以前的帮助),但不适用于段落对象。我知道我可以获取段落文本并将其添加到 listItem,但随后我失去了格式。有没有办法插入一个保留所有格式的段落? (我知道我可以使用var newElement = child.getParent().insertListItem(childIndex, elementContent.getText()) 插入文本而不进行文字格式化)

这里是代码:

function test() {      
  var targetDocId = "1A02VhxOWLUIdl8LTV1tt2S1yASDbOq77VbsUpxPa6vk";  
  var targetDoc = DocumentApp.openById(targetDocId);
  var body = targetDoc.getBody();
  var elementContent = targetDoc.getChild(2); // a paragraph with its formating
  var childIndex = 0;
  for (var p= 0; p< targetDoc.getNumChildren(); p++) {
    var child = targetDoc.getChild(p);    
    if (child.getType() == DocumentApp.ElementType.LIST_ITEM){
      while(child.getType() == DocumentApp.ElementType.LIST_ITEM){
        child = targetDoc.getChild(p)
        Logger.log("child =  " + child.getText())
        childIndex = body.getChildIndex(child);
        Logger.log(childIndex)
        p++
      }
      child = targetDoc.getChild(p-2);
      var listId = child.getListId();
      if (child.getText() == '') {
         childIndex = childIndex -1;
      }
      Logger.log(childIndex)
      var newElement = child.getParent().insertListItem(childIndex, elementContent);
      newElement.setListId(child);
      var lastEmptyItem = targetDoc.getChild(childIndex +1).removeFromParent();      
      break;
    }

这是我的 targetDoc 的屏幕截图(注意第二项,Paragraph):

【问题讨论】:

    标签: google-apps-script google-docs


    【解决方案1】:

    我知道这个问题很老了,但我已经想出了一个解决方案,并将留在这里给任何可能需要它的人。它并不完整,因为我还没有找到将任何内联绘图和方程式复制到新元素的方法......

    无论如何,这是我的代码,如果要转换为列表项的段落只有文本和内联图像,它会很好用。

    function parToList() {
      var doc = DocumentApp.getActiveDocument();
      var body = doc.getBody();
      //gets the paragraph at index 1 on body -> can be changed to what you want
      var par = body.getChild(1);
      var childs = [];
      for (var i = 0; i<par.getNumChildren(); i++) {
        var child = par.getChild(0);
        childs.push(child);
        child.removeFromParent();
      };
      par.removeFromParent();
      //puts the list item on index 1 of body -> can be changed to the wanted position
      var li = body.insertListItem(1, "");
      childs.reverse();
      for (var j in childs) {
        var liChild = childs[j];
        var childType = liChild.getType();
        if (childType == DocumentApp.ElementType.EQUATION) {
          //still need to find a way to append an equation
        } else if (childType == DocumentApp.ElementType.INLINE_DRAWING) {
          //still need to find a way to append an inlineDrawing
        } else if (childType == DocumentApp.ElementType.INLINE_IMAGE) {
          li.appendInlineImage(liChild);
        } else if (childType == DocumentApp.ElementType.TEXT) {
          li.appendText(liChild);
        };
      };
    };
    

    干杯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      • 2022-10-15
      • 2016-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多