[Google Apps脚本getWidth()用于文档表单元格宽度返回null

问题描述 投票:1回答:1

我想使用Google Apps脚本在Google文档中插入图片,并将其(图片)展开到页面的宽度-页边距,或者将其插入到表格单元格中,然后展开到单元格。

代码如下:

var image = DriveApp.getFileById(fileId).getBlob();
var insertPlace = body.findText(searchText).getElement();
insertPlace.asText().setText("");
var insertedImage = insertPlace.getParent().asParagraph().insertInlineImage(0, image);
var h = insertedImage.getHeight();
var w = insertedImage.getWidth();
insertedImage.setWidth(width).setHeight(width * h / w);

问题出在哪里?

宽度!

如何查找表格单元格或主体的宽度?

我尝试过]

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

但是单元格在表的单列中,但为空,并且表采用页边距的所有宽度。

有人知道如何获得单元格的宽度吗?

另外,如何获取页面宽度,页边距等...

更新

我发现问题一定与Google代码有关。实际上,与文档相比,他们对幻灯片中的表格的处理方式有所不同:

https://developers.google.com/apps-script/reference/slides/table-cellhttps://developers.google.com/apps-script/reference/document/table-cell

例如,在幻灯片版本中,有getParentColumn(),该文档不可用于文档...任何许多相似的差异。

[这些人在Google做什么?!?!?

我想通过使用Google Apps脚本在Google文档中插入图片,并将其(图片)展开到页边距的宽度,或者将其插入到表格单元格中,然后展开到。 。

google-apps-script
1个回答
1
投票

这不是完美的方法,但似乎让我非常接近。也许您可以继续使用它,然后再进行其余操作。我以为PAGE_WIDTH和PAGE_HEIGHT以磅为单位,但是当我查看数字时,它似乎很大,因此我将其用作像素。它似乎对我有用,图像位于页面的中心,从左边缘到右边缘。我没有调整页面高度,因为我的图像看起来不错,但是您可能希望这样做。

© www.soinside.com 2019 - 2024. All rights reserved.