Google-apps-script – How to Add RTL Tables to a Google Document

embedgoogle docsgoogle-apps-script

As I'm trying to format google doc tables and in reference to this thread I was able to add some formatting to the header row, to the table data and was able to create a doc in a landscape layout.

However, I still didn't find a solution to:

Making all the table cells to be RTL (it seems like I can only RTL paragraphs)

Here is the code I have so far

function createDoc() {
  var ssUrl = 'https://docs.google.com/spreadsheets/d/1ZWbAOdOfRSJtrfmLWjX6Q6cPT-YztII_uVdsBzWcQ_I/edit#gid=1116846206';
  var sheetName = 'paycall';   // name of sheet to use

   var values = SpreadsheetApp.openByUrl(ssUrl)
                             .getSheetByName(sheetName)
                             .getDataRange()
                             .getDisplayValues();


  var doc = DocumentApp.create('Sample Document 1');
  var body = doc.getBody();
  body.setPageHeight(595).setPageWidth(842);

  var tableStyle = {}; 
   tableStyle[DocumentApp.Attribute.FONT_SIZE] = 10; 

  var rowsData = values;
  body.insertParagraph(0, doc.getName())
      .setHeading(DocumentApp.ParagraphHeading.HEADING1).setLeftToRight(false);
  table = body.appendTable(rowsData).setAttributes(tableStyle);
  table.getRow(0).editAsText().setFontSize(12).setBold(true).a;


}

Best Answer

Based on https://developers.google.com/apps-script/reference/document, there are only two methods to set text direction RTL, one for Class List and another for Class Paragraph. Consider to use one of them to set the text direction to RTL.

Here is a code snippet taken from the answer to Setting Text Direction in Google Scripts

function table(){
  Code: var cells = [ ["EnglishTitle", "HebrewTitle"], ["Text", ""] ]; 
  var tableStyle = {}; 
  tableStyle[DocumentApp.Attribute.FONT_FAMILY] = DocumentApp.FontFamily.TIMES_NEW_ROMAN;
  tableStyle[DocumentApp.Attribute.FONT_SIZE] = 12; 
  var doc = DocumentApp.getActiveDocument().getBody();
  doc.appendTable(cells).setAttributes(tableStyle).getCell(1,1).insertParagraph(0, "Top line not moved. \nText Should Be Right to left, but it's not, \nThis text is correctly on the right.").setLeftToRight(false).appendText('\nThis is the appended text,\ncorrectly set right to left.');
}