【问题标题】:Mail merge: can't append images from template邮件合并:无法从模板附加图像
【发布时间】:2015-06-01 14:34:34
【问题描述】:

我正在尝试创建一个邮件合并功能来创建一个基于简单模板的文档。 我尝试使用以下函数复制模板元素,但我遇到了(内联)图像问题,它们始终显示为 PARAGRAPH 而不是 INLINE_IMAGE,并且出现以下图标而不是图像:

代码如下:

function appendToDoc(src, dst) {
  // iterate accross the elements in the source adding to the destination
  for (var i = 0; i < src.getNumChildren(); i++) {
    appendElementToDoc(dst, src.getChild(i));
  }
}

function appendElementToDoc(doc, object)
{
    var element = object.copy();
    var type = object.getType();

    if (type == DocumentApp.ElementType.PARAGRAPH) {
        doc.appendParagraph(element);
    } else if (type == DocumentApp.ElementType.TABLE) {
      doc.appendTable(element);
    } else if (type== DocumentApp.ElementType.INLINE_IMAGE) { // This is never called :(
       var blob = element.asInlineImage().getBlob();
       doc.appendImage(blob); 
    }
}

关于如何解决这个问题的任何想法?提前致谢!

【问题讨论】:

    标签: google-apps-script google-docs


    【解决方案1】:

    据我所知,段落中包含内联图像,因此我们必须检查段落中的图像类型。

    所以检查代码是这样的:

    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();
            doc.appendImage(blob);
          }
          else doc.appendParagraph(element.asParagraph());
        }
    

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 2014-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-01
      相关资源
      最近更新 更多