1

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

I basically open a document and get its DocumentBodySection, I then iterate through the elements, copy them (Element.copy()) to get a deep detached copy and then append the element to another document.

This works well for paragraph and other element types, but when it comes to inline images, I get a broken link in the resulting document.

Anyone got that to work?

Here is a snippet

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
}

Thanks in advance.

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • I've tried every possible method I could imagine and I always get this same broken link too... the only workaround I found was to copy the whole document using DocsList.copy and remove what I had to from the copy. – Serge insas Jul 02 '12 at 20:31
  • Thank Serge, I have also tried as many ways I could think off, I am planing to bring this up during the next office hours ( I have not found a script issue either ) and then create an issue if they have no idea what to do ... Yes i am going to use a combination of DocList.copy ... InlineDrawings which work well and replace template keys with image blobs. Thanks for trying. – patto_chennai Jul 04 '12 at 10:14
  • 1
    Please do raise this in the issue tracker. – Jan Kleinert Jul 11 '12 at 20:43
  • OK thanks Jan, will do latter in the day ... its 5 am now ;) – patto_chennai Jul 12 '12 at 23:40
  • if I may ask: how did you manage to append InlineDrawings? – Fausto R. Jan 10 '13 at 11:42

1 Answers1

2

I got appendImage to work for me in not the same, but similar case.

Hopefully it will do for you as well, please let me know

Adapting from your code, just add / change the Blob lines to avoid repeated inline images

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

EDIT 1: as commented below, when looping through the bodySection, any INLINE_IMAGE will be embedded in a PARAGRAPH. The code below will work for any INLINE_IMAGE that is not wrapped with text. If that was the case, you need to dig deeper.

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());
        }
Fausto R.
  • 1,314
  • 3
  • 16
  • 29
  • Hi Fausto, could you have a look at [this post](http://stackoverflow.com/questions/14252185/copy-text-images-tables-all-formatting-margins-from-on-gdoc-to-another) and tell me why inline image don't show up ? I used your code snippet but no luck ;-) I get an * instead ... – Serge insas Jan 11 '13 at 20:49
  • I will edit my answer with more details, however the problem in that code (question 14252185) is that an INLINE_IMAGE is always embedded in a new PARAGRAPH, so you has to dig one step (for an image without surrounding text) to get the actual image. – Fausto R. Jan 13 '13 at 00:22
  • Thanks, I'll play with that and let you know what I found. – Serge insas Jan 13 '13 at 00:43
  • Fully working ! thanks a lot... too bad I can't "vote up" twice ^^ – Serge insas Jan 13 '13 at 10:22