如何复制模板并插入其他文档中的内容?

Sky*_*ion 4 copy google-docs-api google-apps-script

我想编写一个复制模板(仅包含容器绑定脚本)的Google Docs脚本,并附加用户选择的另一个文档的内容.我怎么做到这一点?我已经有办法选择文件(模板有一个静态ID),但想办法将文档的所有内容(包括inlineImages和hyperLinks)复制到我的新文档中.

Ser*_*sas 7

我想唯一的方法是逐个复制元素......有一大堆文档元素,但它不应该太难以完全无遗漏.以下是最常见类型的方法,您必须添加其他类型.

(原始代码借鉴了Henrique Abreu的答案)

function importInDoc() {
  var docID = 'id of the template copy';
  var baseDoc = DocumentApp.openById(docID);
  var body = baseDoc.getBody();

  var otherBody = DocumentApp.openById('id of source document').getBody();
  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.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.INLINE_IMAGE )
      body.appendImage(element);

    // add other element types as you want

    else
      throw new Error("According to the doc this type couldn't appear in the body: "+type);
  }
}
Run Code Online (Sandbox Code Playgroud)