Google Apps Script - Manipulate Table in Document

Viewed 18

I've been reading the reference docs for hours now, and I can't figure this out.

(Capitalized words are Google Objects as represented in the reference at https://developers.google.com/apps-script/reference )

Situation: Using data from a different Spreadsheet, a Google Document is created. Using a script bound to that Document, after some template replacements, a Table is created and appended. I easily managed to create the Table with the data I want. By default, the table seems to occupy the entire width of the page, and its columns all have the same width regardless of their content.

Objective: I want the columns of the table to be dynamically adjusted according to the content of the cell with the most content, up to a maximum width.

The problem is that I can't figure out how to do this. Info that I managed to gather so far:

By default, after appending the table:

  • table.getColumnWidth(index) // Always returns null, no matter what. I don't understand why.
  • cycling over the cells and try cell.getWidth() always returns null

BUT: the previous calls do return a value, but only AFTER I first set it with table.setColumnWidth(index, value) or cell.setWidth(value).

Just to clarify:

  const doc = DocumentApp.getActiveDocument();
  const body = doc.getBody();
  const table = body.getTables()[0];

  Logger.log(table.getNumRows()); // OK - Returns the expected value
  Logger.log(table.getRow(0).getNumCells()); // OK - Returns the expected value
  Logger.log(table.editAsText().getText()); // OK- Returns the expected value

// Up to here, everything makes me think the table is correctly formed, but then:

  Logger.log(table.getColumnWidth(0)); // null - why?
  Logger.log(table.getColumnWidth(1)); // null - why? 
  table.setColumnWidth(0, 25);
  Logger.log(table.getColumnWidth(0)); // now it returns 25!!!
  Logger.log(table.getColumnWidth(1)); // null - why? 

  Logger.log(table.getCell(0, 0).getWidth()) // 25
  Logger.log(table.getCell(1, 0).getWidth()) // 25
  Logger.log(table.getCell(0, 1).getWidth()) // null

I don't understand this behaviour.

Important: the columns change according to the data of the spreadsheet. If the columns were always the same I could just find some values via trial and error, and set them. But the columns are not always the same, not in number nor in content. Everything has to be dynamically adjusted to the actual data that is inputted from time to time.

So, to recap, this is what I would like to do, after having appended the table filled with the data (which I can do just fine):

  1. Find a way to get a preferred width value for every column. I'm thiking finding the largest cell for every column
  2. Adjust every column's width according to their content, having a minimum and a maximum value, and preserving the table's total width.

But every width value in a newly appended table seem to be null until I explicitly set it. Also another problem is that the width of the columns seem to be measured in "points". How do I get a string length in "points"?

I was expecting some kind of autoresize function, but the API doesn't seem to offer it. It does for SpreadsheetApp, but this is a Table inside a Document.

I'm really lost here. What am I missing? What am I doing wrong? Any kind of help will be greatly appreciated.

0 Answers
Related