0
votes

I've seen Google say that using Google Apps Script with a Google Document (i.e. word processing file) is akin to using html to design a webpage.

Well, I'd like to push text (from a spreadsheet) and everything else (e.g. formatting, vertical and horizontal alignment of text, photos, tables, etc) to a Google Document.

One question I have, though, is - how do I vertical align all of these elements?For instance, on the cover page I want a title (at top of page, centered horizontally); a photo (middle of page, centered horizontally); and a couple people's names, and two dates listed top to bottom ( all at bottom of page, left-aligned).

Would people suggest that, similar to HTML, I use a table? If so, what height do I give the table? I.e., what is the height of a Google Document page? Why do I not see a way to set table row height?

Any/all help very greatly appreciated!! Thanks!!

1

1 Answers

0
votes

Use the setMinimumHeight(Integer) method of the TableRow Class, to set the minimum height of a table row. Why is there not simply height, or no maximum height? Probably because if you do something like increase the font size of text in a cell, and it's greater than the cell height, the cell will expand.

Google Documentation

First you need to get the body:

var body = DocumentApp.getActiveDocument().getBody();

Then you need to find the place in the document where you want to insert the table:

var foundTag = body.findText("putTextToFindHere");
if (foundTag != null) {
  var tagElement = foundTag.getElement();
  var parent = tagElement.getParent();
  var insertPoint = parent.getParent().getChildIndex(parent);
  //Logger.log('insertPoint: ' + insertPoint);
  var table = body.insertTable(insertPoint, tableData);
  //table.getRow(0).editAsText().setBold(true);

  var columnWidths = [45,65,70,70,105,60,70];
  for (var i = 0;i<columnWidths.length;i++) {
    table.setColumnWidth(i, columnWidths[i]);
  };
};

The above code shows setting the column widths.