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?