首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不能将脚注转换为新文档--整个功能失败

不能将脚注转换为新文档--整个功能失败
EN

Stack Overflow用户
提问于 2020-07-18 19:49:17
回答 1查看 129关注 0票数 0

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

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

代码语言:javascript
运行
复制
Service Documents failed while accessing document with id [id of target doc]

现在,我对不能将脚注转换为新文件的功能没有问题--或者至少只传递脚注的文本。但是,此错误会阻止我的整个函数运行(文档中的任何元素都不会被转移)。

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

这里有一个带有脚注的示例文档

,这是我的代码:

代码语言:javascript
运行
复制
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();
      
    }
  } 
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-23 10:00:36

不幸的是,在现阶段,用Apps脚本编程创建脚注是不可能的

  • 相关的特征请求已经存在,但尚未实现。
  • 这一定是为什么在婴儿车行为中复制带脚注的段落(因此试图在新文档中创建脚注)的原因。
  • 这个问题似乎还在调查中。
  • 同时,如果我们可以在没有脚注的情况下复制文档,最好的解决办法是以编程方式删除脚注。
  • 这可以通过循环遍历段落中的所有子段来完成,如果遇到脚注,请将其删除。

样本:

代码语言:javascript
运行
复制
    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);
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62973123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档