5

I creating a google doc by script and want to insert table to it by this code:

  var row1 = "some city"
  var row2 = "some text"
  var rowsData = [[row1, row2]];
  var table = body.appendTable(rowsData);
  table.setBorderWidth(0);
  style[DocumentApp.Attribute.BOLD] = true;
  style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
  table.setAttributes(style);

I'm expecting that all text in cells will be bold and centered. But I see that only bold attribute applied. I've tried to add some script

 var cell1 = table.getCell(0, 0);
  var cell2 = table.getCell(0, 1);
  cell1.setAttributes(style);
  cell1.editAsText().setAttributes(style);

but no effect.

Please say me how to center text in cell properly!

tehhowch
  • 9,645
  • 4
  • 24
  • 42
Karen Fisher
  • 747
  • 1
  • 8
  • 25

3 Answers3

14

Text alignment can`t be applied on "table-cell" item, it can be allplied only on "paragraph" item. To get "paragraph" from "table-cell", you need to do the following:

var cellStyle = {};
cellStyle[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
tabel.getRow(0).getCell(1).getChild(0).asParagraph().setAttributes(cellStyle)

(thanks to Stefan van Aalst for his answer )

Community
  • 1
  • 1
user1561325
  • 435
  • 4
  • 17
2

Perhaps not doable 5 years ago, when the question was asked. Today, you can use GUI. Select the table (or cells in question), right click on your selection and pick "Table Properties". The pop-up window should look a bit like this:

Docs > Table Properties

You'll find both vertical and horizontal alignment options there.

Bsquare ℬℬ
  • 4,423
  • 11
  • 24
  • 44
Paul S
  • 21
  • 1
0

I believe you want to use .setAlignment instead of .setAttribute or style[].

cell1.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
Dunsel
  • 65
  • 5
  • 1
    there is no such method... there is a .setVerticalAlignment(DocumentApp.VerticalAlignment.CENTER); but not for horizontal alignment. – Serge insas Mar 05 '14 at 23:52