I want to get the width of a table in Google Documents. The following screenshot shows a demo document. It has only a table having one row which has only one cell. The table was added by clicking on Insert > Table and then on the option to insert a table with a single row and single cell. The table was not manipulated after being inserted.
In the Class Table from the Google Apps Script Class Table it hasn't a method to get the width, the Class Row either, but the Class Cell has getWidth(), so I tried to use it but it returns null. I'm the document owner, it's stored in My Unit, I'm using the old runtime (Mozilla Rhino), Chrome, a G Suite account. The project was created from the Docs UI by clicking on Tools > Script editor.
Here is the code (the project doesn't include anything else)
function myFunction() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const tables = body.getTables();
const table = tables[0];
const row = table.getRow(0);
const cell = row.getCell(0);
const width = cell.getWidth();
Logger.log(width);
}
The code was run from the script editor. Here is the log
I already searched the issue tracker for getWidth. Some results were returned but none of them looks to be related.
I already searched SO.
Google Apps Script getWidth() for document table cell width returns null While it could look as a duplicate, it's about inserting an image. By the other hand the OP tried
var width = insertPlace.getParent().asParagraph().asTableCell().getWidth();
but that didn't work either. The current answer is about getting the width of an image inserted from Google Drive.
So the question is How can I get the table width?
If getWidth() is buggy, is there another way to find the table width no only when the table uses the default width but also when it is smaller?


