When I try to make a text right to left in Google Docs, its preview in every android browser, when someone doesn't login, is not OK. I mean an Android browser still shows it in left to right format. Of course after logging in, or when using the Google Docs Android app, there is no such problem. Is there a solution for this problem, for example using a Google Apps Script or so?

I tried this code some friend suggested, but it doesn't work properly!

function makeRight(){
    var body =DocumentApp.getActiveDocument().getBody();
    console.log("HERE BM!")
    //var paraghraphs = DocumentApp.getActiveDocument().getBody().findElement(DocumentApp.ElementType.PARAGRAPH);
    for (var i = 0; i < body.length; i++){
        var ithchild = body.getChild(i)
        if (ithchild.getType() == DocumentApp.ElementType.PARAGRAPH){
            ithchild.setAlignment(DocumentApp.HorizontalAlignment.RIGHT);
        }
    }
}

1 Answers

0
Community On

Looks like you're trying to run your for statement on body.length but this doesn't return a number, so the for statement isn't even running.

I took your code and made some changes then tested on a Google Doc of my own, the below code seems to work as you're expecting.

function makeRight() {
  var body = DocumentApp.getActiveDocument().getBody();
  var numChildren = body.getNumChildren(); //gets number of children for body

  //for each child of body, run the below code
  for (var i = 0; i < numChildren; i++) {
    var ithchild = body.getChild(i).asParagraph();
    if (ithchild.getType() == DocumentApp.ElementType.PARAGRAPH) {
      ithchild.setAlignment(DocumentApp.HorizontalAlignment.RIGHT);
    }
  }
}

Instead of trying to run the code for "body.length", instead I've defined "numChildren" which gets the number of children for the whole document and returns this as an integer:

var numChildren = body.getNumChildren(); //gets number of children for body

You also need to get variable "ithchild" as a paragraph, which is why I added asParagraph() to that line:

var ithchild = body.getChild(i).asParagraph();

This can then be used to run your for statement on the whole document:

for (var i = 0; i < numChildren; i++) {