如何获得桌子宽度 [英] How to get the table width

查看:50
本文介绍了如何获得桌子宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取Google文档中表格的宽度.以下屏幕截图显示了一个演示文档.它只有一张表,其中一行只有一个单元格.该表是通过单击插入>添加的.表格,然后在选项中插入具有单行和单个单元格的表格.插入表后未对其进行操作.

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.

在Google Apps脚本类表的类表中,它既没有获取宽度的方法,也没有类行,但是类单元格具有getWidth(),所以我尝试使用它,但是它返回了.我是文档所有者,它存储在我的单位"中,我使用的是旧版运行时(Mozilla Rhino),Chrome和G Suite帐户.该项目是通过在Google文档界面中点击工具">脚本编辑器.

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

我已经在问题跟踪器中搜索了getWidth.返回了一些结果,但似乎没有任何关联.

I already searched the issue tracker for getWidth. Some results were returned but none of them looks to be related.

我已经搜索过了.

文档表单元格宽度的Google Apps脚本getWidth()返回null 插入图片.另一方面,OP尝试过

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();

但是那也不起作用.当前的答案是关于从Google云端硬盘插入图片的宽度.

but that didn't work either. The current answer is about getting the width of an image inserted from Google Drive.

所以问题是如何获取表格宽度?

如果getWidth()有问题,是否还有另一种方法来查找表宽度,不仅是在表使用默认宽度时,还是在表宽度较小时?

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?

推荐答案

这是已记录的

ja ... @ google.com

ja...@google.com

状态:无法解决(预期行为)

你好, 感谢您的举报.函数"getWidth"被指定为"getWidth".如果单元格的宽度未更改,则返回null.您可以手动更改宽度,也可以使用"setWidth"使其适应图像尺寸的方法. 更多信息: https://developers.google.com/apps -script/reference/document/table-cell#setWidth(Number)

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)

  • 问题#2 :
    • Issue#2:
    • du ... @ google.com

      du...@google.com

      状态:无法解决(预期行为)

      尽管未记录所有这些内容,但新表的预期行为是为未设置的属性检索null.如果选择单元格并修改其格式(字体大小,斜体等),则将获得这些属性的值.

      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.

      对于单元格宽度,在创建新表时,所有单元格均以均匀分布的单元格"形式出现,这意味着它没有固定宽度",如果您修改单元格的宽度,它将更改为固定宽度.如果您修改整个表格的默认宽度,则所有单元格都将更改为固定宽度. Google Doc API的预期行为是仅返回具有固定宽度的单元格的宽度.

      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.

      我实现了以下解决方法,该方法将记录每个单元格的宽度和表格的总宽度(新表格的默认宽度为450磅).

      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).

      解决方法:

      /**
       * 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);
      }
      

      • 代码既不属于我也不属于我自己,而只是来自issuetracker的报价.
      • 这篇关于如何获得桌子宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆