首页 > 解决方案 > Google Script - 如何将文档图像附加在一起?

问题描述

我有以下用于将文档附加在一起的代码块。它工作正常,但每当它尝试附加包含图像的文档时,它都会抛出异常“不允许操作”。关于为什么的想法受到赞赏。

var docIDs = destFolder.getFiles();
  while (docIDs.hasNext()) {
  var file = docIDs.next();
    list.push(file.getId());
}
  var baseDoc = DocumentApp.openById(list[0]);
  var body = baseDoc.getActiveSection();

  for (var i = 1; i < list.length; ++i ) {
    var otherBody = DocumentApp.openById(list[i]).getActiveSection();
    var totalElements = otherBody.getNumChildren();
    for( var j = 0; j < totalElements; ++j ) {
      var element = otherBody.getChild(j).copy();
      var type = element.getType();                                      
      if( type == DocumentApp.ElementType.INLINE_IMAGE )
        body.appendImage(element);
      else if( type == DocumentApp.ElementType.PAGE_BREAK )
        body.appendPageBreak(element);
      else if( type == DocumentApp.ElementType.PARAGRAPH )
        body.appendParagraph(element);

  }
  }

编辑:抛出错误的确切行是 else if( type == DocumentApp.ElementType.PARAGRAPH ) body.appendParagraph(element);

我试图附加在一起的文档是包含一些图像的简单 Google DOC。

标签: google-apps-script

解决方案


推荐阅读