如何复制模板并从其他文档插入内容? [英] How to copy a template and insert content from another document?

查看:279
本文介绍了如何复制模板并从其他文档插入内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要编写一个用于复制模板(仅包含容器绑定脚本)的Google文档脚本,并附加用户选择的另一个文档的内容。我将如何做到这一点?我已经有一个方法来选择文件(模板有一个静态id),但找出一种方法来复制文档的所有内容(包括inlineImages和超链接)到我的新文档。

$ b $我想,唯一的办法是一个一个复制元素...有一大堆的文档元素,但它不应该太难了,相当详尽。



(原始代码借用自由Henrique Abreu回答< a>)

  function importInDoc(){
var docID ='模板副本的ID';
var baseDoc = DocumentApp.openById(docID);
var body = baseDoc.getBody();

var otherBody = DocumentApp.openById('source document'的id)。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);

//根据需要添加其他元素类型

else
throw new Error(根据doc这个类型不能出现在body中: + type);
}
}


I want to write a Google Docs script that copies a template (which just contains a container bound script) and appends the contents of another document chosen by the user. How would I accomplish this? I already have a way to select the file (the template has a static id), but figure out a way to copy all the content of the document (including inlineImages and hyperLinks) to the my new document.

I guess the only way is to copy elements one by one... there are a whole bunch of document elements but it shouldn't be too hard to be quite exhaustive. Here is how it goes for the most common types, you'll have to add the other ones.

(original code borrowed from an answer by 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);
  }
}

这篇关于如何复制模板并从其他文档插入内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆