How to get the table width

眉间皱痕 提交于 2020-08-26 07:24:10

问题


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?


回答1:


This is a recorded

  • Issue#1:

ja...@google.com

Status: Won't Fix (Intended Behavior)

Hello, Thanks for your report. The function "getWidth" returns null if the cell's width hasn't changed. You can manually change the width or use the "setWidth" method to adapt it to the image size. More information: https://developers.google.com/apps-script/reference/document/table-cell#setWidth(Number)

  • Issue#2:

du...@google.com

Status: Won't Fix (Intended Behavior)

Hi,

Despite all this not being documented, the intended behavior for a new table is to retrieve null for the attributes that aren't set. If you select the cell and modify its formatting (font size, italic, etc), then you'll get the values for those attributes.

For the cell width, when a new table is created all cells comes as “evenly distributed cells”, meaning it doesn’t have a “fixed width”, if you modify the width of a cell it’ll change to a fixed width. If you modify the default width of the entire table, all cells will change to a fixed width. The intended behavior for Google Doc API is to return the width only for cells with a fixed width.

I implemented the following workaround that will log you the width of each cell and the total width of the table (the default width for a new table is 450 points).

Workaround:

/**
 * Code written by du....@google.com
 * @see https://issuetracker.google.com/issues/143810853#comment2
 */
function getTabeCellAttributes() {
  var fileId = '[FILE-ID]';
  var textInTableCell = "test";
  var body = DocumentApp.openById(fileId).getBody();  

  var tableRow = body.findText(textInTableCell).getElement().getParent().getParent().getParent().asTableRow();
  var evenlyDistributedCells = 0;
  var defaultTableWidth = 450;
  var totalWidth = 0;
 
  for(var i=0; i<tableRow.getNumChildren(); i++) {
    var tableCell = tableRow.getChild(i).asTableCell();
    var tableCellWidth = tableCell.getWidth();
   
    if(tableCellWidth == null) {
      evenlyDistributedCells++;
    }
    else {
      totalWidth += tableCellWidth;
      defaultTableWidth -= tableCellWidth;
      Logger.log("Width of cell number: " + (i+1) + " is: " + tableCellWidth)
    }
  }
 
  if (evenlyDistributedCells!=0) {
    var evenlyDistributedWidth = defaultTableWidth/evenlyDistributedCells;
    totalWidth += defaultTableWidth;
    Logger.log('There are ' + evenlyDistributedCells + ' evenly distributed cells with a width of: ' + evenlyDistributedWidth)
  }
  else {
    Logger.log("There are no evenly distributed cells, all cells have a fixed width.")
  }
 
  Logger.log('The total width of the table is: ' + totalWidth);
}
  • Code neither belongs to me or written by me, but just a quote from issuetracker.



回答2:


Thanks to @TheMaster for referring to the Issues (I was not able to find them) . Below is a very simple way to get the size of a table that works for a specific case of having a table as a child of the document body.

function myFunction1() {
  const doc = DocumentApp.getActiveDocument();
  const body = doc.getBody();
  const attributes = body.getAttributes();
  const width = attributes[DocumentApp.Attribute.PAGE_WIDTH] - attributes[DocumentApp.Attribute.MARGIN_LEFT] - attributes[DocumentApp.Attribute.MARGIN_RIGHT];
  Logger.log(width);
}

The default page size for me is Letter (width = 8.5 inches) and the margins are 1 inch.

The logged table width was 468 points (1 inch = 72 points) =6.5 inches so the the width is was was expected.



来源:https://stackoverflow.com/questions/63419556/how-to-get-the-table-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!