复制文本,图像,表格的所有格式,利润率从GDoc到另一个(Copy text, images, ta

2019-07-18 01:23发布

尝试一些邮件合并脚本之后,我决定未写我自己。 我的合并脚本运行作为一个单独用过滤器读取从GDoc模板,从GSpreadsheet读取数据,并将其合并到两种或Gmail的到一个新的GDoc - 一个页/每SS行的电子邮件。

问题是,它不会复制文本格式,页边距或图像到Gmail或新GDoc ......只有纯文本。

我使用DocumentApp.openById> getActiveSection>的getText()来捕获文本。

这里是GDoc代码http://goo.gl/fO5vP我似乎无法共享的脚本,所以我不得不把它放在一个文档。 它复制到一个新的脚本,它会进行颜色编码。

Answer 1:

你应该先使用复制的模板DocsList所以你用一个“完整”的初始文档开始。

  var template = DocsList.getFileById(docIDs[0]);// get the template model, in this sample I had an array of possible templates, I took the first one
  var newmodelName=template.substr(0,11)+'multipage'+template.substring(18);// define a new name, do what you need here...
  var baseDocId = DocsList.copy(template,newmodelName).getId();// make a copy of firstelement and give it new basedocname build from the serie(to keep margins etc...)
  var baseDoc = DocumentApp.openById(baseDocId);// this is the new doc to modify

然后使用文档类有直接replaceText方法


编辑:关于你的第二个问题,这里是你如何能做到的建议。 它可以很好地除了inlineImage ,我将继续寻找在此。 您也可以使脚本更普遍加入其它元素类型...

function myFunction() {
  var template = DocsList.getFileById(key);// get the template model
  var newmodelName='testcopy';// define a new name, do what you need here...
  var baseDocId = DocsList.copy(template,newmodelName).getId();// make a copy of firstelement and give it new basedocname build from the serie(to keep margins etc...)
  var baseDoc = DocumentApp.openById(baseDocId);// this is the new doc to modify
  var body = baseDoc.getActiveSection();
  body.appendPageBreak();
  var totalElements = body.getNumChildren();
  for( var j = 0; j < totalElements; ++j ) {
    var element = body.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 )
      { var blob = body.getChild(j).asInlineImage().getBlob();
       body.appendImage(blob); }
  }
}

编辑2个多亏@Fausto ,这里是一个完全正常的版本。 内嵌图像都包含在一个段落,所以我们不得不挖多个等级,以获得BLOB ...

function myFunction() {
  var template = DocsList.getFileById(key);// get the template model
  var newmodelName='testcopy';// define a new name, do what you need here...
  var baseDocId = DocsList.copy(template,newmodelName).getId();// make a copy of firstelement and give it new basedocname build from the serie(to keep margins etc...)
  var baseDoc = DocumentApp.openById(baseDocId);// this is the new doc to modify
  var body = baseDoc.getActiveSection();
  body.appendPageBreak();
  var totalElements = body.getNumChildren();
  for( var j = 0; j < totalElements; ++j ) {
    var element = body.getChild(j).copy();
    var type = element.getType();
    if (type == DocumentApp.ElementType.PARAGRAPH) {
      if (element.asParagraph().getNumChildren() != 0 && element.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_IMAGE) {
        var blob = element.asParagraph().getChild(0).asInlineImage().getBlob();
        body.appendImage(blob);
      }
      else body.appendParagraph(element.asParagraph());
    }
    else if( type == DocumentApp.ElementType.TABLE )
      body.appendTable(element);
    else if( type == DocumentApp.ElementType.LIST_ITEM )
      body.appendListItem(element);
  }
}


文章来源: Copy text, images, tables, ALL formatting, margins from on GDoc to another