问题描述
我正在创建一个函数,该函数会将用户当前正在选择的文本从一个文档传输到另一个文档。
大多数情况下,它工作正常,但是当我尝试转移列表项时,如在此image上看到的那样,列表项号消失了。
在转移到目标文档之后,最后一张图像中的列表项现在看起来像this。
是否有办法确保转移号码,或者至少以有效的方式重新创建号码? 谢谢!
最小可复制示例:
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 {
}
}
}
解决方法
将GlyphType设置为NUMBER
:
body
.appendListItem(element)
.setGlyphType(DocumentApp.GlyphType.NUMBER)