Templates 当应用程序脚本将模板内容追加到文档中时,编号列表将继续
我有一个应用程序脚本,可以将模板文件的内容复制到文档的末尾。它有一个小麻烦:编号的列表从一个副本延续到下一个副本 我有许多不同的模板,用户可以将它们附加到文档末尾。每个模板都存储在自己的文档中Templates 当应用程序脚本将模板内容追加到文档中时,编号列表将继续,templates,google-apps-script,document,numbered-list,Templates,Google Apps Script,Document,Numbered List,我有一个应用程序脚本,可以将模板文件的内容复制到文档的末尾。它有一个小麻烦:编号的列表从一个副本延续到下一个副本 我有许多不同的模板,用户可以将它们附加到文档末尾。每个模板都存储在自己的文档中 function addSub(template_id){ var mainBody = DocumentApp.getActiveDocument().getBody(); var tempBody = DocumentApp.openById(template_id).
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());
}
}
函数addSub(模板id){
var mainBody=DocumentApp.getActiveDocument().getBody();
var tempBody=DocumentApp.openById(template_id).getBody();
对于(var i=0;i您现在知道ListItems有一个ID了吗?它是一个字符串,您可以通过myListItem.getListId()访问它
您的所有列表项似乎都具有相同的ID。
如果是这种情况,编号必须如您所述
为什么它们有相同的ListID?
我不知道。
body.appendListem方法似乎总是选择相同的listId
我还没有测试它,但是您可以尝试将新附加的ListItem的listID设置为原始文档的listID,如果它们不同的话
是的,我知道.copy()方法应该包含此信息,但是body.appendListItem方法可能不关心
因此,您可以尝试首先保存listItem的分离副本。
然后将其附加到新的主体。
然后将新附加的listItem的id设置为分离副本的id。
我知道这很愚蠢,但可能会有帮助。
我还没试过
我对listItems没有什么经验,但到目前为止我看到的是,如果附加或插入listItems,文档体中似乎只有一个ListId
这可能是问题的原因
希望这能有所帮助。Richard Gantz解决问题后,通过以下代码更正:
var listItemDictionary = {};//top
根据Niklas Ternvall/Richard Gantz的回答,我发现了一个更简单的解决方案,适用于每个模板不超过一个列表的情况
function addSub(template_id) {
var mainBody = DocumentApp.getActiveDocument().getBody();
var tempBody = DocumentApp.openById(template_id).getBody();
var listID = null;
for(var i = 0;i<tempBody.getNumChildren();i++){
var element = tempBody.getChild(i).copy();
var type = element.getType();
if(type == DocumentApp.ElementType.TABLE)
mainBody.appendTable(element);
else if(type == DocumentApp.ElementType.PARAGRAPH)
mainBody.appendParagraph(element);
else if(type == DocumentApp.ElementType.LIST_ITEM){
if(listID==null) // First list item
listID = mainBody.appendListItem('temp'); // Define new listID
mainBody.appendListItem(element).setListId(listID); // Apply to copy
}
else if(type == DocumentApp.ElementType.PAGE_BREAK)
mainBody.appendPageBreak(element);
}
mainBody.removeChild(listID); // Delete temporary list item
}
函数addSub(模板id){
var mainBody=DocumentApp.getActiveDocument().getBody();
var tempBody=DocumentApp.openById(template_id).getBody();
var listID=null;
对于(var i=0;iYou是对的。他们确实共享ID。我猜它是从template-document复制的。我花了一点时间才理解这段代码在做什么:每次代码看到一个新的listId时,它都会在目标文档中创建一个新的临时listItem,以获取该文档的新listId(保存临时listItem以避免ID被重用)并将新的listId分配给复制的项。字典保存复制到已创建临时项的项的所有listId,并允许将复制到同一临时项的项的listId映射。最后的代码将清除所有临时listItems。
if(listItemDictionary){//bottom
mainBody.appendParagraph("");
for(var key in listItemDictionary){
listItemDictionary[key].clear().removeFromParent()
}
}
function addSub(template_id) {
var mainBody = DocumentApp.getActiveDocument().getBody();
var tempBody = DocumentApp.openById(template_id).getBody();
var listID = null;
for(var i = 0;i<tempBody.getNumChildren();i++){
var element = tempBody.getChild(i).copy();
var type = element.getType();
if(type == DocumentApp.ElementType.TABLE)
mainBody.appendTable(element);
else if(type == DocumentApp.ElementType.PARAGRAPH)
mainBody.appendParagraph(element);
else if(type == DocumentApp.ElementType.LIST_ITEM){
if(listID==null) // First list item
listID = mainBody.appendListItem('temp'); // Define new listID
mainBody.appendListItem(element).setListId(listID); // Apply to copy
}
else if(type == DocumentApp.ElementType.PAGE_BREAK)
mainBody.appendPageBreak(element);
}
mainBody.removeChild(listID); // Delete temporary list item
}