当Apps脚本将模板内容附加到文档中时,编号列表将继续 [英] Numbered lists continues when Apps Script appends template-content into document
问题描述
我有一个Apps脚本,可将模板文件的内容复制到文档的末尾。它有一个小麻烦:编号列表从一个副本继续到下一个。
我有许多不同的模板,用户可以将其附加到文档的末尾。每个模板都存储在自己的Document中。
function addSub(template_id){
var mainBody = DocumentApp.getActiveDocument ).getBody();
var tempBody = DocumentApp.openById(template_id).getBody();
for(var i = 0; i< tempBody .getNumChildren(); i ++){
var element = tempBody .getChild(i);
if(element.getType()== DocumentApp.ElementType.TABLE)
mainBody.appendTable(element.copy());
else if(element.getType()== DocumentApp.ElementType.PARAGRAPH)
mainBody.appendParagraph(element.copy());
else if(element.getType()== DocumentApp.ElementType.LIST_ITEM)
mainBody.appendListItem(element.copy());
else if(element.getType()== DocumentApp.ElementType.PAGE_BREAK)
mainBody.appendPageBreak(element.copy());
$ b 它可能看起来像这样:(我想要列表重新设置模板的每个新副本)
使用此模板名称的表格
一些原始文本
- 列表item1
- 列表item2
使用此模板名称的表格
一些原始文本
- 列表item1
- 列出item2
解决方案在Richard Gantz解决它之后,它被这段代码纠正了:
var listItemDictionary = {}; // top
...
else if(element.getType()== DocumentApp.ElementType.LIST_ITEM){
var listCopy = element。 copy()。asListItem()
var lcID = listCopy.getListId();
if(listItemDictionary [lcID] == null){
var tempLI = mainBody.appendListItem(temp)
listItemDictionary [lcID] = tempLI;
Logger.log(lcID)
mainBody.insertListItem(childIndex + j,listCopy.setListId(listItemDictionary [lcID]));
...
if(listItemDictionary){// bottom
mainBody.appendParagraph();
for(var key in listItemDictionary){
listItemDictionary [key] .clear()。removeFromParent()
}
}
I have an Apps Script that copies the content of an template-file to the end of a document. It works with one minor annoyance: the numbered list continues from one copy to the next.
I have many different templates that users can append to the end of the document. Each template is stored in its own Document.
function addSub(template_id){
var mainBody = DocumentApp.getActiveDocument().getBody();
var tempBody = DocumentApp.openById(template_id).getBody();
for(var i = 0;i<tempBody .getNumChildren();i++){
var element = tempBody .getChild(i);
if(element.getType() == DocumentApp.ElementType.TABLE)
mainBody.appendTable(element.copy());
else if(element.getType() == DocumentApp.ElementType.PARAGRAPH)
mainBody.appendParagraph(element.copy());
else if(element.getType() == DocumentApp.ElementType.LIST_ITEM)
mainBody.appendListItem(element.copy());
else if(element.getType() == DocumentApp.ElementType.PAGE_BREAK)
mainBody.appendPageBreak(element.copy());
}
}
It could look like this: ( I want the list to reset for each new copy of the template)
table with name of this template
some raw text
- List item1
- List item2
table with name of this template
some raw text
- List item1
- List item2
解决方案 After Richard Gantz solved it, It was corrected by this code:
var listItemDictionary = {};//top
...
else if(element.getType() == DocumentApp.ElementType.LIST_ITEM){
var listCopy = element.copy().asListItem()
var lcID = listCopy.getListId();
if (listItemDictionary[lcID] == null){
var tempLI = mainBody.appendListItem("temp")
listItemDictionary[lcID] = tempLI;
}
Logger.log(lcID)
mainBody.insertListItem(childIndex+j, listCopy.setListId(listItemDictionary[lcID]));
}
...
if(listItemDictionary){//bottom
mainBody.appendParagraph("");
for(var key in listItemDictionary){
listItemDictionary[key].clear().removeFromParent()
}
}
这篇关于当Apps脚本将模板内容附加到文档中时,编号列表将继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!