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!
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.
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 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With