如何复制模板并插入另一个文档中的内容? [英] How to copy a template and insert content from another document?

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

问题描述

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

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.

(从 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天全站免登陆