I’m working on a Google Docs script to create tables automatically. I can make the text bold but I’m having trouble getting the text to center properly in the table cells.
Here’s my current code:
var location = "New York"
var description = "Sample description"
var tableData = [[location, description]];
var newTable = documentBody.appendTable(tableData);
newTable.setBorderWidth(0);
formatting[DocumentApp.Attribute.BOLD] = true;
formatting[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
newTable.setAttributes(formatting);
The bold formatting works fine but the text won’t center. I also tried this approach:
var firstCell = newTable.getCell(0, 0);
var secondCell = newTable.getCell(0, 1);
firstCell.setAttributes(formatting);
firstCell.editAsText().setAttributes(formatting);
But still no luck with centering. What’s the correct way to center align text inside Google Docs table cells using Apps Script?
Had this exact same problem a few months ago - drove me nuts until I cracked it. You’re trying to align the cell itself, but you actually need to align the paragraph inside the cell. Google Docs tables work differently - each cell contains paragraph elements, so that’s where the alignment has to go.
Here’s what works:
var firstCell = newTable.getCell(0, 0);
var paragraph = firstCell.getChild(0).asParagraph();
paragraph.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
You’ll have to do this for every cell. The trick is using getChild(0) to grab the paragraph inside the cell, then applying alignment to that. Works every time for me, no matter what kind of table I’m building. Just make sure you add your content first, then do the alignment - otherwise the timing gets weird and the script might fail.
This is a super common issue with Google Docs tables in Apps Script. Table cells don’t accept horizontal alignment the way you’d expect - you have to target the paragraph inside each cell instead. Here’s what’s happening: setAttributes()
on the table or cell level doesn’t cascade down to paragraph formatting. Each cell has its own paragraph object that handles alignment separately. Try this:
var location = "New York"
var description = "Sample description"
var tableData = [[location, description]];
var newTable = documentBody.appendTable(tableData);
newTable.setBorderWidth(0);
// Center align each cell's content
for (var row = 0; row < newTable.getNumRows(); row++) {
for (var col = 0; col < newTable.getRow(row).getNumCells(); col++) {
var cell = newTable.getCell(row, col);
var para = cell.getChild(0).asParagraph();
para.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
// Apply bold to the paragraph text
para.editAsText().setBold(true);
}
}
This loops through all cells and applies alignment to their paragraph content. I’ve used similar code in several projects and it handles both alignment and text formatting without issues.
yeah, that always got me too when I started out with google docs scriptin. just make sure to center the paragraph elements inside the cells, not the cells themselves. you can use newTable.getCell(0,0).getChild(0).asParagraph().setAlignment(DocumentApp.HorizontalAlignment.CENTER)
for each cell to fix it. it’s a bit finicky but it works.