文档表单元格宽度的Google Apps脚本getWidth()返回null [英] Google Apps Script getWidth() for document table cell width returns null
问题描述
我想使用Google Apps脚本在Google文档中插入图片,并将其(图片)展开到页面的宽度-页边距,或者将其插入到表格单元格中,然后展开到宽度的细胞.
I'd like to insert image in the Google document by using Google Apps Script, and to spread it (the image) to the width of the page - margins or to insert it in the table cell and spread it to the width of the cell.
代码如下:
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);
问题出在哪里?
宽度!
如何找到表格单元格或主体的宽度?
How to find width of the table cell or body?
我尝试过
var width = insertPlace.getParent().asParagraph().asTableCell().getWidth();
但是尽管单元格位于表的单列中,但为空,并且表占据了页边距的所有宽度.
but got null although cell is in the single column of the table, and table takes all the width of the page-margins.
有人知道如何获得单元格的宽度吗?
Anyone got the idea about how to get width of the cell?
此外,如何获取页面宽度,边距等...
Also, how to get page width, margins etc...
更新
我发现问题一定出在与此相关的Google代码上. 实际上,与文档相比,他们对幻灯片中的表格的处理方式有所不同:
I have discovered that the issue must be with the Google code about this. Actually, they have made differently handling with tables in slides compared to document:
https://developers.google.com/apps-script /reference/slides/table-cell https://developers.google.com/apps-script/reference/文档/表格单元格
例如,在幻灯片版本中,有getParentColumn(),该文档不适用于文档...有许多相似的差异.
In slides version there is, for example, getParentColumn(), which is not available for the document... any many similar discrepancies.
这些家伙在Google做什么?!?!?
What are doing these guys at Google?!?!?
推荐答案
这不是十全十美,但似乎让我非常接近.也许您可以继续使用它,然后再进行其余操作.我以为PAGE_WIDTH和PAGE_HEIGHT很好,但是当我看数字时,它似乎很大,因此我将它们用作像素.它似乎对我有用,图像位于页面中心,从左边缘到右边缘.我没有调整页面高度,因为我的图像看起来不错,但是您可能希望这样做.
This is not perfect but it seems to get me pretty close. May be you could work with it some more and get the rest of the way. I thought that the PAGE_WIDTH and PAGE_HEIGHT were in points but when I look at the number it seemed rather large so I used them as pixels instead. And it appears to work for me the image is centered in the page and goes from left margin to right margin. I didn't adjust the page height because my image looked ok but you might wish to do that.
function insertImage() {
var doc=DocumentApp.getActiveDocument();
var body=doc.getBody();
var atts=body.getAttributes();
for(var att in atts) {
Logger.log(att + ':' + atts[att]);
}
var folderId="Folder ID";
var files=DriveApp.getFolderById(folderId).getFiles();
while(files.hasNext()) {
var file=files.next();
if(file.getName()=="Image File Name") {
var image=file.getBlob().getAs('image/png');//may need to change content type
var img=body.appendImage(image);
var h=img.getHeight();
var w=img.getWidth();
var mr=atts['MARGIN_RIGHT'];
var ml=atts['MARGIN_LEFT'];
var sw=atts['PAGE_WIDTH'];
var sh=atts['PAGE_HEIGHT'];
img.setHeight(sw*h/w);
img.setWidth((sh-mr-ml)*w/h);
}
}
}
这篇关于文档表单元格宽度的Google Apps脚本getWidth()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!