无法将脚注转移到新文档中——整个功能失败

Can't transfer footnote into new document -- entire function fails

我正在编写一个函数,通过将每个元素移动到一个新文档中来复制当前文档 -- 它是一个更大项目的一部分。

但是,当文档中有脚注时,函数会失败,出现此错误。

Service Documents failed while accessing document with id [id of target doc]

现在,我对无法将脚注转移到新文档中的功能没有问题——或者至少只转移脚注的文本。但是,此错误使我的整个功能停止 运行(文档中的 none 元素被转移)。

是否可以忽略脚注或将其转换为文本,并允许函数将所有其他元素转移到新文档中?

Here's a sample document with a footnote

这是我的代码:

function duplicateDocument() {
  var currentDoc = DocumentApp.getActiveDocument().getBody();
  var targetDoc = DocumentApp.create('New Doc');
  var totalElements = currentDoc.getNumChildren();
  
  //Goes through each type of element to preserve formatting
  for( var index = 0; index < totalElements; ++index ) {
    var body = targetDoc.getBody();
    var element = currentDoc.getChild(index).copy();
    var type = element.getType();
    
    if( type == DocumentApp.ElementType.PARAGRAPH ){
      body.appendParagraph(element);
    }
    else if( type == DocumentApp.ElementType.TABLE){
      body.appendTable(element);
    }
    else if( type == DocumentApp.ElementType.LIST_ITEM){
      body.appendListItem(element);
      
    } else if( type == DocumentApp.ElementType.BOOKMARK ){
      body.appendBookmark(element);
      
    } else if( type == DocumentApp.ElementType.INLINE_IMAGE ){
      body.appendImage(element);
      
    } else if( type == DocumentApp.ElementType.HORIZONTAL_RULE ){
      body.appendHorizontalRule();
      
    } else if( type == DocumentApp.ElementType.PAGE_BREAK ){
      body.appendPageBreak();
      
    }
  } 
}

遗憾的是,目前无法使用 Apps 脚本以编程方式创建脚注

  • 相关的feature request已经存在,但还没有实现
  • 这一定是复制带有脚注的段落(因此尝试在新文档中创建脚注)导致 buggy behavour
  • 的原因
  • 这个问题似乎还在调查中
  • 同时 - 如果我们可以忍受复制没有脚注的文档 - 最好的解决方法是以编程方式删除脚注
  • 这可以通过遍历段落的所有子项来完成,如果遇到脚注 - 将其删除

样本:

    if( type == DocumentApp.ElementType.PARAGRAPH){
      var num = element.asParagraph().getNumChildren();
      for( var i = 0; i < num; i++ ) {
        var child = element.asParagraph().getChild(i);
        var childType = child.getType();        
        if( childType == DocumentApp.ElementType.FOOTNOTE){
          child.removeFromParent();
        }
      }
      body.appendParagraph(element);
    }