【发布时间】:2019-11-02 21:07:22
【问题描述】:
我想使用 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();
虽然单元格在表格的单列中,但得到了 null,并且表格占据了页边距的所有宽度。
有人知道如何获取单元格的宽度吗?
另外,如何获取页面宽度、边距等...
更新
我发现问题一定出在 Google 代码上。 实际上,与文档相比,他们对幻灯片中的表格进行了不同的处理:
https://developers.google.com/apps-script/reference/slides/table-cell https://developers.google.com/apps-script/reference/document/table-cell
在幻灯片版本中,例如,getParentColumn() 不适用于文档...任何许多类似的差异。
这些家伙在 Google 做什么?!?!?
【问题讨论】:
-
您的第一个链接来自幻灯片,第二个来自文档,它们是不同的方法,因此没有理由 getParent() 应该在两者中。
标签: google-apps-script google-docs grid-layout