首页 > 解决方案 > 列表项目编号不会转移到新文档

问题描述

我正在创建一个函数,它将用户当前正在选择的文本从一个文档传输到另一个文档。

它在大多数情况下都可以正常工作,但是当我尝试传输列表项时,如图所示列表项编号消失了。

传输到目标文档后,最后一张图像中的列表项现在看起来像这样

有没有办法确保号码被转移,或者至少以有效的方式重新创建它们?谢谢!


最小可重现示例:

function sendToDoc() {
  var currentDoc = DocumentApp.getActiveDocument().getSelection();
  
  var targetDoc = DocumentApp.openByUrl(a different documents url);
  var body = targetDoc.getBody();

  //if you are selecting some text, this function will transfer your selection into the target document

  if(currentDoc) {
    var totalElements = currentDoc.getRangeElements();
    
    //for each element in your selection

    for( var index = 0; index < totalElements.length; ++index ) {
      var element = totalElements[index].getElement().copy();
      var type = element.getType();
      
      
      //gets the element type and transfers it over to the target doc

      if( type == DocumentApp.ElementType.PARAGRAPH ){
        body.appendParagraph(element);
      }
      else if( type == DocumentApp.ElementType.TABLE){
        body.appendTable(element);
      }
      else if( type == DocumentApp.ElementType.LIST_ITEM){ //this is where the list items get transferred
        body.appendListItem(element);
      }
      else if( type == DocumentApp.ElementType.INLINE_IMAGE ){
        body.appendImage(element);
      }
      else if( type == DocumentApp.ElementType.HORIZONTAL_RULE ){
        body.appendHorizontalRule();
      }
      else {
        
      }
    }
    
  } 

标签: google-apps-scriptgoogle-docs

解决方案


字形类型设置为NUMBER

 body
  .appendListItem(element)
  .setGlyphType(DocumentApp.GlyphType.NUMBER)

推荐阅读