0

I'm working on a quick way for teachers to create translations of papers hosted on Google Drive using a script. The expected behavior is that the script works its way through the document body elements, translating text, and copying it to a new page appended to the document. This also needs to look for images and copy those in the document flow to the translation.

I have the text translation working - it's grabbing paragraph text and translating into a new page. The images in the document aren't being copied for some reason.

Script

function translate() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();

  body.appendPageBreak();

  var elements = body.getNumChildren();
  for( var i=0;i<elements;i++) {
   var element = body.getChild(i).copy();
    var type = element.getType();
    if( type == DocumentApp.ElementType.PARAGRAPH ){
      var text = element.asParagraph().getText();
      var spn = LanguageApp.translate(text, 'en', 'es');
      body.appendParagraph(spn);
     }
    else if( type == DocumentApp.ElementType.INLINE_IMAGE ){
      var img = element.asInlineImage().getBlob();
      body.appendImage(img);
    }
  }
}

I found this SO post which iterates over a document and prints the elements in the console (which was also helpful for my logic). The test script shows the second paragraph with an INLINE_IMAGE, but my else if catch isn't copying it to the new page. Text is carried down fine.

Should I run a different test in the logic to get images as well as text?

Community
  • 1
  • 1
Brian
  • 4,274
  • 2
  • 27
  • 55
  • Does this answer your question? [Mail merge: can't append images from template](https://stackoverflow.com/questions/30576269/mail-merge-cant-append-images-from-template) – Rubén Aug 05 '20 at 15:27

1 Answers1

0

As you mentioned in your question the inline image is in the second paragraph, you should be able to get the image by getting the paragraph first and then getting the image within that paragraph. Check my post in this question for more details on this.

Hope that helps!

Community
  • 1
  • 1
KRR
  • 4,647
  • 2
  • 14
  • 14
  • Thanks for the recommendation. After getting home and chewing on it over dinner, I came to the same conclusion - I overlooked the fact that the image is a child of the paragraph. Got the loop fixed now. – Brian Aug 13 '15 at 00:40