google-apps-script - 将文本、图像、表格、所有格式、边距从 GDoc 复制到另一个

标签 google-apps-script google-docs

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

问题是它不会将文本格式、边距或图像复制到 Gmail 或新的 GDoc 中……只有纯文本。

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

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

最佳答案

您应该首先使用 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);
  }
}

关于google-apps-script - 将文本、图像、表格、所有格式、边距从 GDoc 复制到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14252185/

相关文章:

javascript - 从 Mac 和 Windows 操作系统中的 Chrome 扩展程序获取 Google 文档中选定/突出显示的文本

regex - 在 Google Doc Apps 脚本中使用 RegEx 来替换文本

google-apps-script - Google 脚本 - 在 Google 文档表中插入图像

javascript - 使用 Gmail 应用脚本按月对标签计数进行分组

javascript - 字符串以 Google Script 开头

google-apps-script - 获取 Google Docs 文件中 ListItem 的父级

google-apps-script - 为什么来自 Google 电子表格脚本的 MailApp.sendEmail 不起作用?

wordpress - 如何使用Google Apps脚本获取Wordpress管理页面

javascript - 单击新窗口,在新窗口中更改元素

javascript - Google Apps 脚本中日期的 oldValue