将文本,图像,表格,所有格式,边距从GDoc复制到另一个

时间:2013-01-10 06:40:34

标签: google-apps-script

尝试了几个邮件合并脚本后,我决定自己编写。我的合并脚本作为单独的文件运行,从GDoc读取模板,从GSpreadsheet读取数据,并将其合并到Gmails或新的GDoc - 每个SS行一页/电子邮件。

问题在于它不会将文本格式,边距或图像复制到Gmail或新GDoc中......只能复制纯文本。

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

以下是GDoc http://goo.gl/fO5vP中的代码 我似乎无法共享一个脚本,所以我不得不把它放在一个文档中。将其复制到一个新脚本,它将被彩色编码。

1 个答案:

答案 0 :(得分:3)

您应首先使用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

然后使用具有直接document class

replaceText method

编辑:关于你的第二个问题,这里有关于你如何做的建议。除了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);
  }
}
相关问题