Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align text in google docs table cell

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!

like image 598
Karen Fisher Avatar asked Mar 05 '14 14:03

Karen Fisher


People also ask

How do you center text in a table in Google Docs cell?

You can make this change by selecting the data that you want to center, then clicking the Center Align button in the toolbar above the document. You can also center text by selecting the text, choosing the Format tab at the top of the window, then choosing the Align & indent option, then choosing Center.


1 Answers

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 )

like image 173
user1561325 Avatar answered Sep 30 '22 16:09

user1561325