如何在 Google Docs 之间复制内容和格式? [英] How to copy content and formatting between Google Docs?

查看:41
本文介绍了如何在 Google Docs 之间复制内容和格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要复制一个 Google 文档的内容,并将其附加到另一个文档中.如果我使用这样的东西:

I need to copy the content of a Google Document, and append it to another Document. If I use something like this:

newDoc.getBody().appendParagraph(template.getText());

newDoc.getBody().appendParagraph(template.getText());

...我得到了文本,但丢失了原始文件中的格式.(粗体斜体等)

...I get the text, but lose the formatting that was in my original file. (Bold, Italic, etc.)

如何将内容和格式复制到新文档中?是否可以将所有内容都分配给一个变量,然后将其复制/粘贴到新文档中?

How can I copy the contents and formatting to the new document? Is it possible to assign everything to one variable, and copy / paste it to the new document?

推荐答案

不能只使用 1 个变量,你必须迭代文档中的所有元素并一个一个地复制它们.

Not using only 1 variable , you'll have to iterate all the elements in the doc and copy them one by one.

同一主题有多个线程,例如尝试这个:如何使用谷歌应用程序脚本复制文档的一个或多个现有页面

there are multiple threads on the same subject, try for example this one : How to copy one or more existing pages of a document using google apps script

只需仔细阅读代码并添加您应该在文档中遇到的所有内容类型(表格、图像、分页符...)

just read carefully the code and add all the content types that you are supposed to meet in your document (tables, images, pagebreaks...)

这是一个关于这个想法的试验(开始)

EDIT : here is a trial on that idea (to start with)

function copyDoc() {
  var sourceDoc = DocumentApp.getActiveDocument().getBody();
  var targetDoc = DocumentApp.create('CopyOf'+DocumentApp.getActiveDocument().getName());
//  var targetDoc = DocumentApp.openById('another doc ID');
  var totalElements = sourceDoc.getNumChildren();

  for( var j = 0; j < totalElements; ++j ) {
    var body = targetDoc.getBody()
    var element = sourceDoc.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);
      }
//    ...add other conditions (headers, footers...
    }
  targetDoc.saveAndClose();
}

这篇关于如何在 Google Docs 之间复制内容和格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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