gpt4 book ai didi

google-apps-script - 如何获取表格宽度

转载 作者:行者123 更新时间:2023-12-02 02:42:02 29 4
gpt4 key购买 nike

我想获取 Google 文档中表格的宽度。下面的截图显示了一个演示文档。它只有一张表,其中一行只有一个单元格。通过单击“插入”>“表格”,然后单击“插入包含单行和单个单元格的表格”选项来添加表格。该表在插入后未进行任何操作。

在 Google Apps 脚本类表的类表中,它没有获取宽度的方法,也没有类行,但类单元有 getWidth(),所以我尝试使用它但它返回null。我是文档所有者,它存储在我的单元中,我使用旧的运行时(Mozilla Rhino)、Chrome、G Suite 帐户。该项目是通过单击“工具”>“脚本编辑器”从文档 UI 创建的。

这是代码(该项目不包含其他任何内容)

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

代码是从脚本编辑器运行的。这是日志

我已经在问题跟踪器中搜索了 getWidth。返回了一些结果,但它们看起来都不相关。

我已经搜索过了。

Google Apps Script getWidth() for document table cell width returns null虽然它看起来像是重复的,但它是关于插入图像的。另一方面,OP尝试了

var width = insertPlace.getParent().asParagraph().asTableCell().getWidth();

但这也不起作用。当前的答案是获取从 Google 云端硬盘插入的图像的宽度。

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

如果getWidth()有问题,是否有另一种方法可以找到表格宽度,不仅当表格使用默认宽度时,而且当表格较小时?

最佳答案

这是录制的

[email protected]

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)

[email protected]

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 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4327366d6d6d6d03242c2c242f266d202c2e" rel="noreferrer noopener nofollow">[email protected]</a>
* @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);
}
  • 代码既不属于我,也不是我编写的,只是来自 issuestracker 的引用。

关于google-apps-script - 如何获取表格宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63419556/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com