1

Stemming from a previous question, I'm creating an Add On for Google Docs which takes the body of a document and translates it as a new page in the same doc. I have the script running and it's translating correctly, but I'm having trouble with the logic to keep the formatting.

This post from last year works well when copying elements in the same language. But, the LanguageApp in Google requires a string to translate.

I simplified the script linked above to copy a document within itself:

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

  Logger.log(body.getAttributes());
  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) {
      // This block copies formatting correctly
      // Pastes English and Spanish into the doc (see img 1).
      body.appendParagraph(element);
      var spn = LanguageApp.translate(element.getText(), 'en', 'es');
      body.appendParagraph(spn);

      // This block pastes Spanish only with no formatting (see img 2)
      // This block is normally commented out when the script runs for testing
      var spn = LanguageApp.translate(element.getText(), 'en', 'es');
      body.appendParagraph(spn)
    }
  }
}

Img1 - text formats correctly, pastes both English and Spanish

Img1 http://www.brianbennett.org/images/coderesult1.png

Img2 - removing the English append, Spanish still translates but loses formatting because of string requirement.

Img2 http://www.brianbennett.org/images/coderesult2.png

I would like help with the logic within the function. I'm thinking the only way to do this - because of the string requirement - is to go line by line, copying the English, translating on a new line, then removing the English. Is there a better way to translate the text and keep the formatting?

Community
  • 1
  • 1
Brian
  • 4,274
  • 2
  • 27
  • 55
  • 1
    Could you post examples (images would do) showing the problem? Nudge - if [that post](http://stackoverflow.com/questions/17575863/how-do-i-format-text-i-am-copying-from-google-document-in-google-app-script/17577431#17577431) helped, why not upvote it? – Mogsdad Aug 26 '15 at 15:49
  • At school right now, so I can't post images...will do so tonight. And I thought I'd already upvoted. Thanks for the reminder. – Brian Aug 26 '15 at 18:02
  • @Mogsdad Just updated with cleaner code and some images showing the input and result from running the script under both circumstances. – Brian Aug 26 '15 at 20:23
  • Cool - I'll have a look this evening, unless someone else figures it out first! – Mogsdad Aug 26 '15 at 20:26

1 Answers1

1

Since machine translation is not a word-for-word operation, it's reasonable to assume that formatting for each translated paragraph will be homogeneous (i.e. the whole paragraph will have the same formatting).

Given that, you can copy most of the format of a paragraph using Paragraph.getAttributes(), then apply them to the new paragraph with Paragraph.setAttributes().

Something like:

...
if(type == DocumentApp.ElementType.PARAGRAPH) {
  // get formatting attributes
  var attributes = element.getAttributes();      

  var spn = LanguageApp.translate(element.getText(), 'en', 'es');
  var newParagraph = body.appendParagraph(spn);

  // Copy format to translated paragraph
  newParagraph.setAttributes(attributes)
}
...
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • I hadn't tried setting attributes with a second variable - I had been chaining it onto `body.appendParagraph(spn)` with mixed results. I'll play around some more during my planning hour today and see what ends up working. – Brian Aug 27 '15 at 10:29
  • It's been a while since I messed around with these functions; I also remember them being finicky. You may need to examine the returned attributes looking for font styles, then set them explicitly. – Mogsdad Aug 27 '15 at 11:31
  • I think it's the string being passed that doesn't carry the attributes. It's not a big deal in the long run. I don't have a *ton* of formatting to redo, so I'm not going to go through and read it line by line. Thanks for the suggestion anyways. – Brian Aug 27 '15 at 15:00