无法获取DocumentBodySection.appendImage(InlineImage)才能

2019-10-16 22:25发布

https://developers.google.com/apps-script/class_documentbodysection#appendImage

我基本上打开文档并获得其DocumentBodySection,然后我通过迭代的元素,将它们复制(Element.copy()),以获得深分离副本,然后将元素追加到另一个文档。

这非常适用于段落和其他元素类型,但是当涉及到内嵌图像,我得到的结果文档中断开的链接。

任何人都得到了工作?

这里是一个片段

function appendSection(doc, section) {
  for (i=0; i<section.getNumChildren(); i++) {
    var el = section.getChild(i);
    if (el.getType() == DocumentApp.ElementType.PARAGRAPH)
    {
      if (el.getNumChildren() != 0) {
        var el_child = el.getChild(0);
        if (el_child.getType() == DocumentApp.ElementType.INLINE_IMAGE)
        {
          doc.appendImage(el_child.asInlineImage().copy());
          Logger.log("Image");
        }
      }
      doc.appendParagraph(el.copy());
      Logger.log("Paragraph");
    }
  }     
  return doc
}

提前致谢。

Answer 1:

我appendImage到不一样的,但类似的情况下,为我工作。

希望它会为你做的一样好,请让我知道

从您的代码适应,只需添加/更改斑点线,以避免重复内嵌图片

  if (el_child.getType() == DocumentApp.ElementType.INLINE_IMAGE)
  {
    var blob = el_child.asInlineImage().getBlob();
    doc.appendImage(blob);
  } 

EDIT 1:如下评论的,通过bodySection循环时,任何INLINE_IMAGE将被嵌入在一个段落。 下面的代码会为不与文本包裹任何INLINE_IMAGE工作。 如果是这样的话,你需要深入挖掘。

for (var elem = 0; elem < bodySection.getNumChildren(); elem++) {
          var theElem = bodySection.getChild(elem).copy();
          if (theElem.getType() == DocumentApp.ElementType.PARAGRAPH) {
            if (theElem.asParagraph().getNumChildren() != 0 && theElem.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_IMAGE) {
              var blob = theElem.asParagraph().getChild(0).asInlineImage().getBlob();
              targetDoc.appendImage(blob);
            }
            else targetDoc.appendParagraph(theElem.asParagraph());
          }
          if (theElem.getType() == DocumentApp.ElementType.LIST_ITEM) targetDoc.appendListItem(theElem.asListItem().copy());
          if (theElem.getType() == DocumentApp.ElementType.TABLE) targetDoc.appendTable(theElem.asTable());
        }


文章来源: Unable to get DocumentBodySection.appendImage(InlineImage) to function properly?