Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Set Horizontal Alignment On A Table In Apps Script

I am not able to find a way to horizontally align a table in a Google Doc using Google Apps Script. I have thoroughly checked all of the documentation, and also blindly tried several approaches.

Attempt One:

var cells = [
  ['Company', rowData[3]],
  ['Title', rowData[4]],
];

var tableStyle = {};
tableStyle[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.RIGHT;

var mentorTable = body.appendTable(cells);

var myTable = body.appendTable(cells);
myTable.setAttributes(tableStyle);

Attempt Two:

var cells = [
  ['Company', rowData[3]],
  ['Title', rowData[4]],
];

var mentorTable = body.appendTable(cells);
myTable.setAlignment(DocumentApp.HorizontalAlignment.RIGHT);

The Google Docs UI supports changing this attribute from the "Table Properties" menu option.

Any thoughts on how to align a table using Google Apps Script?

like image 673
SeanMaday Avatar asked Oct 20 '22 06:10

SeanMaday


1 Answers

I realize this question is relatively old, but I found the following worked for me. Rather than setting the alignment directly on the cell, I need to set it the child.

// create table
table = body.appendTable();

// add row
var tr = table.appendTableRow();

// add cell and right align
var td = tr.appendTableCell('hello world!');
td.getChild(0).asParagraph().setAlignment(DocumentApp.HorizontalAlignment.RIGHT);
like image 96
MatAff Avatar answered Oct 22 '22 00:10

MatAff