Google apps script DocumentApp服务抛出一个;访问id为“0”的文档时服务文档失败;错误

Google apps script DocumentApp服务抛出一个;访问id为“0”的文档时服务文档失败;错误,google-apps-script,google-docs,Google Apps Script,Google Docs,我正在为GoogleDocs创建一个脚本,它将当前文档中的每个元素复制到另一个文档中(基本上是复制) duplicateDocument(); 函数重复文档(){ var currentDoc=DocumentApp.getActiveDocument().getBody(); var targetDoc=DocumentApp.create('Speech Doc'); var totalElements=currentDoc.getNumChildren(); //遍历每种类型的元素以保留格

我正在为GoogleDocs创建一个脚本,它将当前文档中的每个元素复制到另一个文档中(基本上是复制)

duplicateDocument();
函数重复文档(){
var currentDoc=DocumentApp.getActiveDocument().getBody();
var targetDoc=DocumentApp.create('Speech Doc');
var totalElements=currentDoc.getNumChildren();
//遍历每种类型的元素以保留格式
对于(var指数=0;指数
我一调用该函数,就会弹出以下错误:

访问id为[目标文档id]的文档时,服务文档失败。


这个代码在一两天前起作用了。。。给了我什么?

问题是我在我想复制的文件中添加了一个脚注

去掉脚注后,它很好地复制了文件


编辑:我在谷歌的

上发表了一篇文章,我的脚本创建了一个新文档,名为“语音文档”。错误中的ID与创建的新文档的ID匹配。对不起,我应该说得更清楚。它没有指定行;它只是说访问文档时出错。文档ID用于名为“Speech doc”的文档,因此我假设它指的是此行:var targetDoc=DocumentApp.create('Speech doc');或者这个:var body=targetDoc.getBody();出于某种原因,调试器在显示错误后就消失了。它没有指定哪一行,只是说它不能访问语音文档。哦,我只是在想,也许你应该先使用saveAndClose(),然后再获取id,这样你就可以访问代码的其余部分。显示的第一行是
duplicateDocument()这是您项目中的实际代码行吗?
duplicateDocument();

function duplicateDocument() {
  var currentDoc = DocumentApp.getActiveDocument().getBody();
  var targetDoc = DocumentApp.create('Speech 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);
    }
    } 
}